2

When I compare Workbook objects that ought to be identical I get False rather than True. Here's an example:

$ import openpyxl
$ w1 = openpyxl.load_workbook('Foo.xlsx')
$ w2 = openpyxl.load_workbook('Foo.xlsx')
$ w1 == w2
False

What's going on?

Bonus question: Is there a way I can compare workbooks? (I'm writing unit tests for a script that manipulates Excel.)

twsh
  • 237
  • 2
  • 8

1 Answers1

5

When you call "load_workbook" it creates a new instance of the openpyxl workbook object. The variable is pointing to a location in memory where that object is stored.

When you have python to compare w1 to w2 it can only compare the reference to a memory location. The memory address can be seen in idle by just typing the variable name.

>>> wb1 = load_workbook('master.xlsx')
>>> wb2 = load_workbook('master.xlsx')
>>> wb1 == wb2
False
>>> wb1
<openpyxl.workbook.workbook.Workbook object at 0x03ED3B50>
>>> wb2
<openpyxl.workbook.workbook.Workbook object at 0x04AD7E30>

If you want to verify that one workbook is the same as another you would need to write your own code that looks deeper into each object. One way to do this would be to use a hash. (See: https://stackoverflow.com/a/16876405/2535649)

Community
  • 1
  • 1
John Steel
  • 324
  • 1
  • 7
  • 1
    That's helpful and set me in the right direction. I wrote something that turns workbooks into dictionaries and then compares them. – twsh Jul 24 '14 at 22:45
  • 1
    @Tom comparing workbooks in general is pretty difficult. You'd have to specify what parts of the workbooks you want to compare: are the same if the have worksheets with the same names? Should the worksheets be the same? etc. – Charlie Clark Jul 26 '14 at 11:14
  • @user2385133 I wanted the result to be `True` iff they had the same number of worksheets with identical names and identical content. (But, in the context I wanted to write a test I'm happy to assume that both have one worksheet with the same name.) – twsh Jul 26 '14 at 11:43
  • 1
    `wb1.get_sheet_names() == wb2.get_sheet_names()` would be a start. Comparing all the cells in all the worksheets would be the next step but could take a long time. A full comparison would have to consider things like charts and styles as well in order not to give false positives. – Charlie Clark Jul 26 '14 at 15:49