Python Forum

Full Version: I am getting a KeyError, after file save, My goal is to after changes save file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is my Code:
import openpyxl
from openpyxl import load_workbook

#load existing excel file from the storage
wb = load_workbook("test1.xlsx")
print(wb._active_sheet_index)

#Print sheet name you want to activate
activate_yoursheet = input("Enter your sheet name to activate:")

#List all sheetnames
for sheet in wb:
    #print(sheet.title)
    if(sheet.title == activate_yoursheet):
        break

wb.active = activate_yoursheet
print("Your activated sheet is now:", wb._active_sheet_index)

wb.save("test1.xlsx")
The Traceback I get is this:
Error:
Traceback (most recent call last): File "xcel.py", line 6, in <module> wb = load_workbook("test1.xlsx") File "C:\Python37\lib\site-packages\openpyxl-2.5.0-py3.7.egg\openpyxl\reader\e xcel.py", line 177, in load_workbook src = archive.read(ARC_CONTENT_TYPES) File "C:\Python37\lib\zipfile.py", line 1428, in read with self.open(name, "r", pwd) as fp: File "C:\Python37\lib\zipfile.py", line 1467, in open zinfo = self.getinfo(name) File "C:\Python37\lib\zipfile.py", line 1395, in getinfo 'There is no item named %r in the archive' % name) KeyError: "There is no item named '[Content_Types].xml' in the archive"
I didn't try anything new, what I thought is it may be a problem with a type of file I am using, i.e. binary or may UTF type of file will work but I don't know how to proceed further.
An xlsx file is a zipped archive containing a hierarchy of folders. In the root folder, there is a file named [Content_Types].xml. The openpyxl module is telling you that this file is missing, meaning that the xlsx file is corrupted. You could perhaps try to open the file with Excel or LibreOffice and save it again.