Why are different worksheet parents not allowed?

69 views
Skip to first unread message

Maksim Penguin

unread,
Aug 14, 2023, 3:48:14 AM8/14/23
to openpyxl-users
Hi, 
i am implementing a tool to merge worksheets. 

So basically I am reading from a couple of worksheets (from different workbooks) and writing everything to another worksheet in another workbook?

While trying to implement i had to take some journeys into the source code of openpyxl because i was often running into bush of ValueErrors raised individually from different places due to mismatches of the worksheet parent workbook. Why is it necessary to check? what are the expected issues? 
I wonder if im missing somehting since i mostly just monkey patched the checks and everything worked as expected™. 

I'm didn't dive very deep yet so excuse my naive question. I guess it has to do with the matter that the parent instance contains stuff like the styles,tables. etc.
So another question araises at my head: why those attributes are not in the worksheet directly by rather linked there from the workbook instance?

I choose to monkey patch the WorksheetCopy class but i think the right solution would be to write a proper __copy__ in the WorkSheet class. I didn't tried that because i think this would require a design change as implied by my last question.

Just to clarify I'm not flaming I'm asking out of honest curiosity

Charlie Clark

unread,
Aug 14, 2023, 4:11:10 AM8/14/23
to openpyxl-users
On 14 Aug 2023, at 9:48, Maksim Penguin wrote:

> Hi,
> i am implementing a tool to merge worksheets.
>
> So basically I am reading from a couple of worksheets (from different
> workbooks) and writing everything to another worksheet in another workbook?
>
> While trying to implement i had to take some journeys into the source code
> of openpyxl because i was often running into bush of ValueErrors raised
> individually from different places due to mismatches of the worksheet
> parent workbook. Why is it necessary to check? what are the expected
> issues?
> I wonder if im missing somehting since i mostly just monkey patched the
> checks and everything worked as expected™.

Styles are workbook globals. As a result worksheets have to be bound to specific workbooks and can't be easily moved around.

Charlie

--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Sengelsweg 34
Düsseldorf
D- 40489
Tel: +49-203-3925-0390
Mobile: +49-178-782-6226

Maksim Penguin

unread,
Aug 15, 2023, 6:15:15 AM8/15/23
to openpyx...@googlegroups.com
I'm confused, since there is just a unidirectional relationship from worksheets to the styles. It should be to hard to just take the styles with you. Which is basically what the WorksheetCopy class is doing.

Still there's a check which raises a exception if the parent of source and target don't match

Charlie Clark

unread,
Aug 15, 2023, 6:29:00 AM8/15/23
to openpyx...@googlegroups.com
On 15 Aug 2023, at 12:15, Maksim Penguin wrote:

> I'm confused, since there is just a unidirectional relationship from worksheets to the styles. It should be to hard to just take the styles with you. Which is basically what the WorksheetCopy class is doing.
>
> Still there's a check which raises a exception if the parent of source and target don't match

I'm sorry, if you don't think the library works the way you think it should. Feel free to work with your monkey patch.

Maksim Penguin

unread,
Aug 15, 2023, 10:00:13 AM8/15/23
to openpyx...@googlegroups.com
As I said I am asking with honest interest and just trying to understand the thought processes. My intend of my above comment is to make sure I didn't miss some more complex issues that went into this.

Also I would be very happy to help. If you think there's potential I would definitely like to purpose a pull request.
To be on the safe side i wanted to discuss my concerns first.

Charlie Clark

unread,
Aug 15, 2023, 1:48:33 PM8/15/23
to openpyx...@googlegroups.com
I don't really know what you're trying to do but, having worked on combining worksheets from different workbooks, I can tell you that the potential problems with styles are considerable and a completely different approach is required for a performant solution.

The copier was deliberately designed with simplicity and reliability in mind, and this is why its scope is limited. One of the key decisions we take on this kind of feature is whether it's still really just library work or is it "application like"?

The copier does the trickiest work: cell data. Most other items can be copied fairly easily in client code. It's arguable that the class hasn't been updated since it was written but I've also seen very few requests for extension and there are problems with quite a few elements due to the nature of binding.
Reply all
Reply to author
Forward
0 new messages