pylightxl icon indicating copy to clipboard operation
pylightxl copied to clipboard

pylightxl.Database.ws_names returns incorrect sheet names in some cases

Open flc opened this issue 2 years ago • 1 comments

Pylightxl Version: 1.60 Python Version: 3.9.14

Code to reproduce the problem:

import pylightxl

path = 'test_sheet.xlsx'
db = pylightxl.readxl(fn=path)
print(db.ws_names)

The output is:

["'#3969'", '#3969']

However the file contains only 1 sheet named #3969. The weird thing is that if I rename the sheet to sheet for example, save it in LibreOffice (see the attached test_sheet_rename.xlsx), then I get from the above code what is expected: ws_names return only 1 sheet named sheet.

test_sheet.xlsx test_sheet_rename.xlsx

flc avatar Oct 13 '22 19:10 flc

The problem seems to be the difference between these two in xl\workbook.xml:

<sheet name="#3969" sheetId="1" state="visible" r:id="rId2"/>
<definedName function="false" hidden="true" localSheetId="0" name="_xlnm._FilterDatabase" vbProcedure="false">&apos;#3969&apos;!$D$1:$D$963</definedName>

because #3969 != &apos;#3969&apos; aka '#3969'

Maybe some excel programs put the sheet name between the apostrophes? At least in the xml they write. But I don't know which ones. It's weird.

But for some reason other parsers can handle this situation:

xlrd==1.2.0:

In [1]: import xlrd
In [2]: wb = xlrd.open_workbook('test_sheet.xlsx')
In [4]: wb.sheet_names()
Out[4]: [u'#3969']

openpyxl==3.0.10:

In [1]: import openpyxl
In [2]: wb = openpyxl.load_workbook(filename='test_sheet.xlsx')
In [4]: wb.sheetnames
Out[4]: ['#3969']

flc avatar Oct 18 '22 08:10 flc

I also noticed this as a regression when moving from 1.59 to 1.60, working with an Excel spreadsheet exported by an inventory tool.

alexlawriewood avatar Dec 29 '22 09:12 alexlawriewood

@flc and @alexlawriewood Thank you for bringing this to our attention! Sorry about the delay, but i just had a chance to look into this and i am rolling out 1.61 that will fix this issue!

PydPiper avatar Jan 01 '23 19:01 PydPiper

Hi @PydPiper, thanks very much for the fix! However, I briefly checked the fixed code and I wonder what happens if a sheet name actually contains a single quote. I think that would lead to an incorrect behavior again.

flc avatar Jan 03 '23 20:01 flc