Python 使用openpyxl 2.6.2读取Excel文件时出错(在openpyxl 2.5.12下工作)

Python 使用openpyxl 2.6.2读取Excel文件时出错(在openpyxl 2.5.12下工作),python,excel,openpyxl,Python,Excel,Openpyxl,我编写了一个脚本,检查Excel文件是否包含工作表“预算”。这是与Python 3.7和openpyxl 2.5.12一起使用的。但是,由于Anaconda将openpyxl更新为2.6.2版,它不再工作(请参阅下面的错误消息)。我在解决此错误消息时遇到问题。感谢您的帮助 # Import modules: import os import pandas as pd from openpyxl import load_workbook from pathlib import Path, Pure

我编写了一个脚本,检查Excel文件是否包含工作表“预算”。这是与Python 3.7和openpyxl 2.5.12一起使用的。但是,由于Anaconda将openpyxl更新为2.6.2版,它不再工作(请参阅下面的错误消息)。我在解决此错误消息时遇到问题。感谢您的帮助

# Import modules:
import os
import pandas as pd
from openpyxl import load_workbook
from pathlib import Path, PureWindowsPath

# Set correct path to Excel files:

excel_folder = PureWindowsPath("C:\\Users\\c3post\\Desktop\\Excel")
correct_excel_folder = Path(excel_folder)
print('Working directory: ', correct_excel_folder, '\n\n')

# Change current working directory so that opening relative paths will work:
os.chdir(correct_excel_folder)

# Add files to list:
files = os.listdir(correct_excel_folder)

# Filter 'xlsx' files and add them to new list:
files_xlsx = [f for f in files if f[-4:] == 'xlsx']

# Filter out xlsx files that do not contain the worksheet "Budget"
files_budget = []

for file in files_xlsx:
    wb = load_workbook(file)
    ws = wb.worksheets
    if 'Budget' in str(ws):
        files_budget.append(file)

print('Files that contain worksheet "Budget": ', files_budget, '\n\n')
print('Files that do not contain worksheet "Budget": ', set(files_xlsx) - set(files_budget))
错误消息:


keyrerror回溯(最近一次调用)
在里面
29
30对于文件中的文件\u xlsx:
--->31 wb=加载工作簿(文件)
32 ws=wb.worksheets
33如果str(ws)中有“预算”:
加载工作簿中的~/anaconda3/lib/python3.7/site-packages/openpyxl/reader/excel.py(文件名、只读、保留vba、仅数据、保留链接)
310 reader=ExcelReader(文件名、只读、保留vba、,
311数据(仅限,保持链接)
-->312读卡器
313 return reader.wb
读取中的~/anaconda3/lib/python3.7/site-packages/openpyxl/reader/excel.py(self)
272 self.read_主题()
273 apply_样式表(self.archive,self.wb)
-->274.自我阅读工作表()
275 self.parser.assign_names()
276如果不是self.read_only:
阅读工作表中的~/anaconda3/lib/python3.7/site-packages/openpyxl/reader/excel.py(self)
226 ws._rels=rels
227 ws_parser=工作表执行器(仅限ws、fh、self.shared_字符串、self.data_)
-->228 ws_解析器.bind_all()
229
230#将任何注释分配给单元格
~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet//\u reader.py in bind\u all(self)
401
402 def bind_all(自身):
-->403 self.bind_单元格()
404 self.bind_merged_cells()
405自绑定超链接()
~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet//\u reader.py在绑定单元格中(self)
324
325 def绑定_单元(自身):
-->326对于idx,self.parser.parse()中的行:
327对于行中的单元格:
328 style=self.ws.parent.\u单元格\u样式[cell['style\u id']]
解析中的~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet//\u reader.py(self)
148元素。清除()
149 elif标记\名称==行\标记:
-->150行=self.parse_行(元素)
151元素。清除()
152产量行
~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet//\u reader.py在parse\u行中(self,row)
270如果有钥匙!=设置(['r','spans'])和键!=集合(['r']):
271#除非有相关信息,否则不要创建标注对象
-->272自身行维度[attrs['r']]=attrs
273
274个单元格=[self.parse_单元格(el)表示行中的el]
KeyError:'r'
请参见
KeyError                                  Traceback (most recent call last)
<ipython-input-2-fa1881e560e0> in <module>
     29 
     30 for file in files_xlsx:
---> 31     wb = load_workbook(file)
     32     ws = wb.worksheets
     33     if 'Budget' in str(ws):

~/anaconda3/lib/python3.7/site-packages/openpyxl/reader/excel.py in load_workbook(filename, read_only, keep_vba, data_only, keep_links)
    310     reader = ExcelReader(filename, read_only, keep_vba,
    311                         data_only, keep_links)
--> 312     reader.read()
    313     return reader.wb

~/anaconda3/lib/python3.7/site-packages/openpyxl/reader/excel.py in read(self)
    272         self.read_theme()
    273         apply_stylesheet(self.archive, self.wb)
--> 274         self.read_worksheets()
    275         self.parser.assign_names()
    276         if not self.read_only:

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

~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet/_reader.py in bind_all(self)
    401 
    402     def bind_all(self):
--> 403         self.bind_cells()
    404         self.bind_merged_cells()
    405         self.bind_hyperlinks()

~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet/_reader.py in bind_cells(self)
    324 
    325     def bind_cells(self):
--> 326         for idx, row in self.parser.parse():
    327             for cell in row:
    328                 style = self.ws.parent._cell_styles[cell['style_id']]

~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet/_reader.py in parse(self)
    148                 element.clear()
    149             elif tag_name == ROW_TAG:
--> 150                 row = self.parse_row(element)
    151                 element.clear()
    152                 yield row

~/anaconda3/lib/python3.7/site-packages/openpyxl/worksheet/_reader.py in parse_row(self, row)
    270         if keys != set(['r', 'spans']) and keys != set(['r']):
    271             # don't create dimension objects unless they have relevant information
--> 272             self.row_dimensions[attrs['r']] = attrs
    273 
    274         cells = [self.parse_cell(el) for el in row]

KeyError: 'r'