使用openpyxl读取.xlsx文件时出错

时间:2019-10-25 16:47:26

标签: python list openpyxl xlsx

使用openpyxl很难读取.xlsx文件。抱歉,在陈述我的原始问题时并没有非常高兴。 以下是代码:

`import openpyxl
 from openpyxl import load_workbook
 workbook = openpyxl.load_workbook(filename='file_name.xlsx')`

以下是追溯:

 `3 from openpyxl import load_workbook
  4 workbook = openpyxl.load_workbook \
----> 5     (filename='file_name.xlsx')

  ~/anaconda3/lib/python3.7/site-packages/openpyxl/reader/excel.py in load_workbook(filename, read_only, keep_vba, data_only, keep_links)
315     reader = ExcelReader(filename, read_only, keep_vba,
316                         data_only, keep_links)
--> 317     reader.read()
318     return reader.wb

~/anaconda3/lib/python3.7/site-packages/openpyxl/reader/excel.py in read(self)
277         self.read_theme()
278         apply_stylesheet(self.archive, self.wb)
--> 279         self.read_worksheets()
280         self.parser.assign_names()
281         if not self.read_only:

~/anaconda3/lib/python3.7/site-packages/openpyxl/reader/excel.py in read_worksheets(self)
225                 ws._rels = rels
226                 ws_parser = WorksheetReader(ws, fh, self.shared_strings, self.data_only)
--> 227                 ws_parser.bind_all()
228 
229             # assign any comments to cells

~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet/_reader.py     in bind_all(self)
406 
407     def bind_all(self):
--> 408         self.bind_cells()
409         self.bind_merged_cells()
410         self.bind_hyperlinks()

~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet/_reader.py in bind_cells(self)
331         for idx, row in self.parser.parse():
332             for cell in row:
--> 333                 style =     self.ws.parent._cell_styles[cell['style_id']]
334                 c = Cell(self.ws, row=cell['row'],      column=cell['column'], style_array=style)
  335                 c._value = cell['value']

TypeError: list indices must be integers or slices, not str` 

1 个答案:

答案 0 :(得分:0)

我认为问题在于cell['style_id']将返回一个字符串,而_cell_styles[]则期望使用整数作为索引。

您是否尝试查看cell['style_id']中的内容,并且在需要时转换为整数?