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
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 683 Aug-29-2021, 12:39 PM
Last Post: snippsat
  Verification of an excel file saleenahohn 2 447 Jul-09-2021, 06:56 AM
Last Post: Larz60+
  Read and write active Excel file euras 4 762 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  Open an excel file Newbie1114 1 799 Jun-16-2021, 09:11 PM
Last Post: Gribouillis
  How to open MIDI-file and get events in a list? philipbergwerf 7 1,988 May-29-2021, 08:24 AM
Last Post: j.crater
  Filter Excel and Convert an Excel File giddyhead 0 778 May-13-2021, 06:31 PM
Last Post: giddyhead
  Problems with inserting images into an Excel File FightingFarmer 2 969 May-12-2021, 10:03 PM
Last Post: FightingFarmer
  Copy column from one existing excel file to another file mkujawsk 0 906 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  Importing text file into excel spreadsheet with formatting david_dsmn 1 905 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  Error on open of file created with tempfile.TemporaryDirectory() Brian177 4 1,625 Apr-05-2021, 07:12 PM
Last Post: Brian177

Forum Jump:

User Panel Messages

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