Reading merged cells

3,668 views
Skip to first unread message

viha...@gmail.com

unread,
Aug 25, 2011, 5:12:14 AM8/25/11
to openpyx...@googlegroups.com
Hello,

how do I read merged cells in an xlsx file?

Thanks

Eric

unread,
Aug 25, 2011, 5:14:03 AM8/25/11
to openpyx...@googlegroups.com
Hello,
it seems to me that if you have for instance A1:B3 merged, then the content of the merged cell will be considered as being in A1.

Cheers,
Eric

2011/8/25 <viha...@gmail.com>

viha...@gmail.com

unread,
Aug 25, 2011, 5:36:12 AM8/25/11
to openpyx...@googlegroups.com
True, but how do I know the range? say A1:B3 is merged, is there a way to indentify it?

viha...@gmail.com

unread,
Aug 25, 2011, 6:04:05 AM8/25/11
to openpyx...@googlegroups.com
to be more precise, something like getMergeCells in PHPExcel, which gives a list of all merged cell ranges

Eric

unread,
Aug 25, 2011, 6:29:45 AM8/25/11
to openpyx...@googlegroups.com
Well, there is no direct function to do that, but we can make one quite easily by reading named ranges info, etc...

2011/8/25 <viha...@gmail.com>

viha...@gmail.com

unread,
Aug 25, 2011, 6:31:43 AM8/25/11
to openpyx...@googlegroups.com
thanks eric, but can you give me some more hints towards that direction. Currently I am trying to read PHPExcel to find how I can implement such a function for openpyxl, but since I am a newbie with openpyxl, I am lost. 

Eric

unread,
Aug 25, 2011, 6:58:16 AM8/25/11
to openpyx...@googlegroups.com
Sorry, I was wrong. I double-checked and there is actually nothing yet done for handling whether a cell is merged or not. However, it's just a property to read from the worksheet (mergeCells XML attribute).

Maybe you can add a ticket to the bugtracker and I'll see what I can do ?

Cheers

2011/8/25 <viha...@gmail.com>

viha...@gmail.com

unread,
Aug 25, 2011, 7:15:21 AM8/25/11
to openpyx...@googlegroups.com
Thanks, just added a ticket (#69)

Eric

unread,
Aug 25, 2011, 7:26:35 AM8/25/11
to openpyx...@googlegroups.com
Okay :) I'm really drowning under work those days, but if I can find a couple of minutes I'll try to code that ;-)

Cheers

2011/8/25 <viha...@gmail.com>

Vihang Patel

unread,
Aug 25, 2011, 7:29:47 AM8/25/11
to openpyx...@googlegroups.com
Thanks again eric

Cheers

Sent on my BlackBerry®


From: Eric <eric....@gmail.com>
Date: Thu, 25 Aug 2011 13:26:35 +0200
Subject: Re: [openpyxl-users] Reading merged cells

viha...@gmail.com

unread,
Aug 25, 2011, 8:46:03 AM8/25/11
to openpyx...@googlegroups.com
Eric,

I have made a patch. I am not sure if this is the best implementation since I am new to your codebase, but it works


In reader/worksheet.py add

-------------

def filter_mergecells((event, element)):


def read_mergecells(ws, xml_source):

    source = _get_xml_iter(xml_source)
    ws.mergeCells = []
    it = iterparse(source)
    for event, element in ifilter(filter_mergecells, it):
        ref = element.get('ref')
        ws.mergeCells.append(ws.range(ref))
        # to avoid memory exhaustion, clear the item after use
        element.clear()

    return

def read_worksheet(xml_source, parent, preset_title, string_table,
                   style_table, workbook_name = None, sheet_codename = None):
    """Read an xml worksheet"""
    if workbook_name and sheet_codename:
        ws = IterableWorksheet(parent, preset_title, workbook_name,
                sheet_codename, xml_source)
    else:
        ws = Worksheet(parent, preset_title)
        fast_parse(ws, xml_source, string_table, style_table)
    read_mergecells(ws, xml_source)
    return ws

--------------------



Eric

unread,
Aug 25, 2011, 8:48:59 AM8/25/11
to openpyx...@googlegroups.com
That's a good start, thanks. I'll try to integrate it further when I have time, but if it works for you now, then it's good :)

Thanks a lot ! :)
Cheers

2011/8/25 <viha...@gmail.com>

viha...@gmail.com

unread,
Aug 25, 2011, 12:06:21 PM8/25/11
to openpyx...@googlegroups.com
Just finished testing. Works fine. 

Cheers
Reply all
Reply to author
Forward
0 new messages