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
  Copy xml content from webpage and save to locally without special characters Nik1811 14 938 Mar-26-2024, 09:28 AM
Last Post: Nik1811
  Open/save file on Android frohr 0 333 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,132 Dec-14-2023, 08:03 AM
Last Post: shanoger
  I am getting a valueError. And not sure why? My goal is to visualize the correlation ReadytoCode 0 477 Dec-11-2023, 05:33 AM
Last Post: ReadytoCode
  Goal Seek tdutcher05 1 792 Nov-17-2023, 10:33 PM
Last Post: deanhystad
  save a session in idle akbarza 5 779 Nov-06-2023, 08:36 AM
Last Post: snippsat
  how to save to multiple locations during save cubangt 1 556 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 772 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  change directory of save of python files akbarza 3 890 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  How can I change the uuid name of a file to his original file? MaddoxMB 2 932 Jul-17-2023, 10:15 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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