pylightxl
pylightxl copied to clipboard
pylightxl.Database.ws_names returns incorrect sheet names in some cases
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
.
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">'#3969'!$D$1:$D$963</definedName>
because
#3969
!= '#3969'
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']
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.
@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!
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.