Python Forum
I am getting a KeyError, after file save, My goal is to after changes save file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting a KeyError, after file save, My goal is to after changes save file
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Correct/proper way to create save files snakes 0 437 Mar-11-2025, 06:58 PM
Last Post: snakes
  How to write variable in a python file then import it in another python file? tatahuft 4 870 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  what is solution to save and reload a object? eue 4 1,776 Nov-23-2024, 02:15 PM
Last Post: DeaD_EyE
  print/save on pdf starwhale 3 849 Nov-08-2024, 10:59 PM
Last Post: Gribouillis
  Read TXT file in Pandas and save to Parquet zinho 2 1,204 Sep-15-2024, 06:14 PM
Last Post: zinho
  Compare 2 files for duplicates and save the differences cubangt 2 922 Sep-12-2024, 03:55 PM
Last Post: cubangt
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,025 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 830 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  RimWorld Save Game Manager- Help! LastStopDEVS 2 1,264 Jul-29-2024, 03:36 AM
Last Post: LastStopDEVS
  How to change a tkinter label with scale and how to save that new value for later? bkeith 3 1,841 Jun-28-2024, 01:35 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020