我已经编写了一个脚本,在MacOS上使用openpyxl读取.xlsx文件。
我做了更新以允许我的脚本在MacOS或Linux上运行。调用load_workbook
时,Openpyxl会引发错误。
import platform
system = platform.system()
if system == "Darwin":
cpgatt = "/Users/tlombard/desktop/py-sandbox/PickleJar/CSG_CHG_Clarity_attributes1.xlsx"
if system == 'Linux':
cpgatt = "/ws/tlombard-sjc/Picklejar/CSG_CHG_Clarity_attributes1.xlsx"
cpgatt_wb = openpyxl.load_workbook(cpgatt)
我得到的错误是:
"TypeError: __init__() got an unexpected keyword argument 'quotePrefix'"
The full traceback:-------
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/auto/elb_hwtools/cae/lib/python/v2.7.8/openpyxl/reader/excel.py", line 149, in load_workbook
_load_workbook(wb, archive, filename, read_only, keep_vba)
File "/auto/elb_hwtools/cae/lib/python/v2.7.8/openpyxl/reader/excel.py", line 237, in _load_workbook
keep_vba=keep_vba)
File "/auto/elb_hwtools/cae/lib/python/v2.7.8/openpyxl/reader/worksheet.py", line 324, in read_worksheet
fast_parse(ws, xml_source, shared_strings, style_table, color_index)
File "/auto/elb_hwtools/cae/lib/python/v2.7.8/openpyxl/reader/worksheet.py", line 312, in fast_parse
parser.parse()
File "/auto/elb_hwtools/cae/lib/python/v2.7.8/openpyxl/reader/worksheet.py", line 93, in parse
dispatcher[tag_name](element)
File "/auto/elb_hwtools/cae/lib/python/v2.7.8/openpyxl/reader/worksheet.py", line 189, in parse_row_dimensions
self.parse_cell(cell)
File "/auto/elb_hwtools/cae/lib/python/v2.7.8/openpyxl/reader/worksheet.py", line 134, in parse_cell
cell = Cell(self.ws, column, row, **style)
TypeError: __init__() got an unexpected keyword argument 'quotePrefix'
>>> cpgatt
'/ws/tlombard-sjc/Picklejar/CSG_CHG_Clarity_attributes1.xlsx'
>>>
答案 0 :(得分:2)
我有同样的问题,这是我的完整痕迹:
Traceback (most recent call last):
File "__init__.py", line 318, in readExcelFile
wb2 = load_workbook( unicode(self.ui_sourcePath.text()))#self.ui_sourcePath.text().replace("/", "\\"))
File "C:\Python27\lib\site-packages\openpyxl\reader\excel.py", line 149, in load_workbook
_load_workbook(wb, archive, filename, read_only, keep_vba)
File "C:\Python27\lib\site-packages\openpyxl\reader\excel.py", line 237, in _load_workbook
keep_vba=keep_vba)
File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 325, in read_worksheet
fast_parse(ws, xml_source, shared_strings, style_table, color_index)
File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 313, in fast_parse
parser.parse()
File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 93, in parse
dispatcher[tag_name](element)
File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 190, in parse_row_dimensions
self.parse_cell(cell)
File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 135, in parse_cell
cell = Cell(self.ws, column, row, **style)
TypeError: __init__() got an unexpected keyword argument 'quotePrefix'
正如您在跟踪中看到的,当openpyxl尝试解析工作簿时发生错误。我在C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py
@ 135
column, row = coordinate_from_string(coordinate)
print column, row
cell = Cell(self.ws, column, row, **style)
问题来自我的Excel工作簿中的一个单元格,而且只有一个。我对此问题的临时解决方案是删除quotePrefix
dict中的de style
键:
style = {}
if style_id is not None:
style_id = int(style_id)
style = self.styles[style_id]
if "quotePrefix" in style.keys():
del style["quotePrefix"]
column, row = coordinate_from_string(coordinate)
cell = Cell(self.ws, column, row, **style)
self.ws._add_cell(cell)
我无法找到这个特定单元格导致这些问题的原因以及如何覆盖引号前缀。