pylightxl icon indicating copy to clipboard operation
pylightxl copied to clipboard

Database.nr(name="SomeNameThatExists") always fails with KeyError

Open bRitch022 opened this issue 2 years ago • 4 comments

Names are redacted to prove the point -- SomeNameThatExists is an actual name on the SheetThatExists sheet of a .xlsx file. I've confirmed it shows up in the pylightxl.Dictionary._ws as well by printing the contents of the _ws just before this call.

Snippet:

import pylightxl as xl

wb = xl.readxl("excel_file.xlsx")
print(wb.nr(name="SomeNameThatExists"))

Output:

Traceback (most recent call last):
  File "<....>/lib/python3.8/site-packages/pylightxl/pylightxl.py", line 1430, in ws
    return self._ws[ws]
KeyError: "'SheetThatExists'"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<....>/lib/python3.8/site-packages/pylightxl/pylightxl.py", line 1611, in nr
    return self.ws(ws).range(address, output=output)
  File "/home/britchie/.local/lib/python3.8/site-packages/pylightxl/pylightxl.py", line 1432, in ws
    raise UserWarning('pylightxl - Sheetname ({}) is not in the database'.format(ws))
UserWarning: pylightxl - Sheetname ('SheetThatExists') is not in the database

I found that the following change resolved this issue, but I'm not sure that this is the most appropriate fix:

- return self._ws[ws]
+ return self._ws[ws.strip('\'')]

bRitch022 avatar Aug 04 '22 23:08 bRitch022

@bRitch022 thank you for considering using pylightxl for your project and submitting this issue. We will take a look to see if we can replicate the issue on our end and will get bet to you shortly

PydPiper avatar Aug 05 '22 04:08 PydPiper

Great! Let me know if I can provide any further clarification. Thanks for being so prompt. I’ve applied the patch to my current project

bRitch022 avatar Aug 05 '22 04:08 bRitch022

@bRitch022 Hey i finally had some time to sit down and dig into this a bit and it looks like this is not a bug from my end. Judging from your traceback it looks like you have named one of your sheets 'SheetThatExists' with the ' in the name thus that is what you will also have to use when trying to key it from wb.ws. You can confirm this if you run wb.ws_names. Let me know if you are seeing something different, but the only way i was able to recreate this is if i included the ' in the excel worksheet name, in which case it should be part of the name.

PydPiper avatar Aug 06 '22 01:08 PydPiper

@bRitch022 Hey i finally had some time to sit down and dig into this a bit and it looks like this is not a bug from my end. Judging from your traceback it looks like you have named one of your sheets 'SheetThatExists' with the ' in the name thus that is what you will also have to use when trying to key it from wb.ws. You can confirm this if you run wb.ws_names. Let me know if you are seeing something different, but the only way i was able to recreate this is if i included the ' in the excel worksheet name, in which case it should be part of the name.

Odd. I'm pretty certain my sheet name doesn't include single quotes in it. After the weekend I'll send the version info of the workbook that's causing the issue to happen and run another test case as well.

bRitch022 avatar Aug 06 '22 14:08 bRitch022