Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Open an Excel file
#1
Hi all,
I am trying to open an excel file (LibreOffice) with the following code:
import openpyxl
wb = openpyxl.load_workbook(r'C:\Users\user\Desktop\Python\example.xlsx')
but i get the follwing error:
Error:
KeyError: "There is no item named '[Content_Types].xml' in the archive"
Can someone please tell what this error means and to read my Excel file.

Thanks
Reply
#2
I use Pandas.
import pandas as pd
df = DataFrame.read_excel('f1.xlsx')
df.head()
Reply
#3
didn't work

import pandas as pd
df = DataFrame.read(r'C:\Users\Desktop\Python\example.xlsx')
Error:
Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> df = DataFrame.read(r'C:\Users\mlakhili\Desktop\Python\example.xlsx') NameError: name 'DataFrame' is not defined
then tried:
from pandas import DataFrame
df = DataFrame.read(r'C:\Users\Desktop\Python\example.xlsx')
Error:
Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> df = DataFrame.read(r'C:\Users\mlakhili\Desktop\Python\example.xlsx') AttributeError: type object 'DataFrame' has no attribute 'read'
The strange thing is that i tried openpyxl on another computer with office installed on it and it worked with :
openpyxl.load_workbook(r'C:\Users\Desktop\Python\example.xlsx')
does it mean it didnt work, because on my other computer i have LibroOffice instean of microsoft office ?
Reply
#4
make sure that the particular file is not corrupted. I have no problem with openpyxl on Linux Mint with xlsx files cretaed/opened/saved with LibreOffice
the error says that the file [Content_Types].xml is missing (Office Open XML format is actually just a zip file) so you can peek what's inside

and jeffsummers's example has an error
df = pd.read_excel(r'C:\Users\Desktop\Python\example.xlsx')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
As i said in my home computer i don't have microsoft office.
i use libre office. i have renamed the extention to xlsx as intructed in the book i am ready. So maybe that's why openpyxl doesn't work.
in my office computer it works just fine.

trying your proposal
df = pd.read_excel(r'C:\Users\Desktop\Python\example.xlsx')
i get the following error:
Error:
Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> df = pd.read_excel(path) File "C:\Users\mlakhili\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel io = ExcelFile(io, engine=engine) File "C:\Users\mlakhili\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\excel\_base.py", line 821, in __init__ self._reader = self._engines[engine](self._io) File "C:\Users\mlakhili\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\excel\_xlrd.py", line 20, in __init__ import_optional_dependency("xlrd", extra=err_msg) File "C:\Users\mlakhili\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\compat\_optional.py", line 92, in import_optional_dependency raise ImportError(msg) from None ImportError: Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.
Reply
#6
You need to install certain optional dependencies when working with excel files. Now this error may explain also the previous error.
The dependency for opening xlsx files is openpyxl (presumably already installed). In your case it asks for xlrd which is the dependency for reading old xls format.

(Feb-18-2020, 02:19 PM)Friend Wrote: i have renamed the extention to xlsx

By any chance is it possible that old format (xls) or some other format is saved as file with extension xlsx? By simply changing the extension you don't change the file format.
You don't need neither MS Office, nor Libre Office installed to work with excel files from python
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Yes i changed the file extension from come libre Office extention to xlsx. But as i said it was proposed in the book i am ready. And i can still open the file normally and edit it as if it was a real MS office file. But anyhow i will check it agin with the original extention.

You said i don't need neitehr MS Office nor LibreOffice to work with Excel files.
i don't get you here. I mean i want to open files, edit, write, and see how things work
Reply
#8
What was the file extension of the file before you change it? I don't know what oind of crap book you read or how you misunderstood the advise.
Find real xlsx file if you want to use openpyxl. Or use appropriate package for the file format.
You don't need office package to work with the files from python (read the file, access content, data, objects, etc. process them). If you want to open the file in the office suite and SEE what happened - then yes, you need them
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Libre Office has its own format (.ods) for spreadsheets. You can't just change the extension.

From Libre Office choose Save As and in the Save as Type drop down pick the most recent Microsoft Excel in there (my copy it is 97/2000/XP).
Reply
#10
(Feb-18-2020, 03:43 PM)buran Wrote: I don't know what oind of crap book you read or how you misunderstood the advise.

The crap book is: Automate the boring stuff with Python from Al Sweigart and maybe i misunderstood the advise, thanks anyway

(Feb-18-2020, 04:34 PM)jefsummers Wrote: Libre Office has its own format (.ods) for spreadsheets. You can't just change the extension.

From Libre Office choose Save As and in the Save as Type drop down pick the most recent Microsoft Excel in there (my copy it is 97/2000/XP).

ahhh now it worked, many many thanks :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 249 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 346 Feb-07-2024, 12:24 PM
Last Post: Viento
  Open/save file on Android frohr 0 277 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 938 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 752 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  How can i combine these two functions so i only open the file once? cubangt 4 804 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 2,837 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 769 Feb-16-2023, 08:11 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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