openpyxl问题Keyerror Content_Types.xml

问题描述 投票:0回答:2

我用 openpyxl 读取代码时遇到问题,直到昨天它还可以工作,现在它不再工作了,我也尝试使用备份副本,但我遇到了同样的问题,我删除了 excel 文件... 我也在另一台电脑上尝试过,也出现同样的错误。 这是代码:

# Writing on a EXCEL FILE
filename = (f"{myPath}/Monatsplan {userfinder} {month} {year}.xlsx")
dienstorinfo = string1
emptycell = ' '   
x = len(dienstorinfo)
if x == 0:
    dienstorinfo = tagesinfo2
try:
    wb = load_workbook(filename)
    ws = wb.worksheets[0]  # select first worksheet
except FileNotFoundError:
    headers_row = ['Datum','Dienst','Funktion','Von','Bis','Schichtdauer','Bezahlte Zeit','Überzeit','Sonnats Zulage','Nachtdienst']
    wb = Workbook()
    ws = wb.active
wb.save(filename)
ws.append([soup_datum,dienstorinfo,soup_funktion,(soup_dienstbegin),(soup_dienstende),(soup_schichtdauer),(soup_bezahltezeit),emptycell,emptycell])

for cols in ws.iter_cols(  ):
     if cols[-1].value:
        cols[-1].border = Border(left=Side(style='thin'),right=Side(style='thin'),top=Side(style='thin'),bottom=Side(style='thin'))                                
ws.column_dimensions['A'].width = 11
ws.row_dimensions['1'].height = 25
ws.column_dimensions['B'].width = 60   
ws.column_dimensions['C'].width = 2   
ws.column_dimensions['D'].width = 3   
ws.column_dimensions['E'].width = 3   
ws.column_dimensions['F'].width = 3   
ws.column_dimensions['H'].width = 3        
ws.column_dimensions['I'].width = 2   
ws.column_dimensions['L'].width = 2   
wb.save(filename)              
wb.close()

a 这是回复:

Traceback (most recent call last):
  File "c:\Users\Diriye\OneDrive\Python\tuxapp\Monatseint_Tool_v1.1.py", line 408, in <module>
    scraping()
  File "c:\Users\Diriye\OneDrive\Python\tuxapp\Monatseint_Tool_v1.1.py", line 364, in scraping
    excel()

    Traceback (most recent call last):
  File "c:\Users\Diriye\OneDrive\Python\tuxapp\Monatseint_Tool_v1.1.py", line 408, in <module>
    scraping()
  File "c:\Users\Diriye\OneDrive\Python\tuxapp\Monatseint_Tool_v1.1.py", line 364, in scraping
    excel()
  File "c:\Users\Diriye\OneDrive\Python\tuxapp\Monatseint_Tool_v1.1.py", line 340, in excel
    wb = load_workbook(filename)
  File "C:\Users\Diriye\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook
    reader.read()
  File "C:\Users\Diriye\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\openpyxl\reader\excel.py", line 276, in read
    self.read_manifest()
  File "C:\Users\Diriye\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\openpyxl\reader\excel.py", line 134, in read_manifest
    src = self.archive.read(ARC_CONTENT_TYPES)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\zipfile.py", line 1463, in read
    with self.open(name, "r", pwd) as fp:
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\zipfile.py", line 1502, in open
    zinfo = self.getinfo(name)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\zipfile.py", line 1429, in getinfo
    raise KeyError(
KeyError: "There is no item named '[Content_Types].xml' in the archive"
python excel openpyxl
2个回答
0
投票

我创建的问题是 .ods 文件类型,并将其重命名为 .xlsx 文件。如果您或其他人属于这种情况 为了解决这个问题,您需要将 .ods 转换为 .xlsx,我使用了在线转换器,它对我有用。 https://www.zamzar.com/convert/ods-to-xls/

确保指定 .xlsx 作为文件类型


0
投票

就我而言,这个错误是因为这段代码:

sheet.row_dimensions[row_in_excel_file].hidden = False

row_in_excel_file 的类型为“str”,但应该是“int”

此代码解决问题: sheet.row_dimensions[int(row_in_excel_file)].hidden = False

© www.soinside.com 2019 - 2024. All rights reserved.