Python Forum
Open an Excel file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Open an Excel file (/thread-24514.html)



Open an Excel file - Friend - Feb-17-2020

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


RE: Open an Excel file - jefsummers - Feb-18-2020

I use Pandas.
import pandas as pd
df = DataFrame.read_excel('f1.xlsx')
df.head()



RE: Open an Excel file - Friend - Feb-18-2020

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 ?


RE: Open an Excel file - buran - Feb-18-2020

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')



RE: Open an Excel file - Friend - Feb-18-2020

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.



RE: Open an Excel file - buran - Feb-18-2020

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


RE: Open an Excel file - Friend - Feb-18-2020

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


RE: Open an Excel file - buran - Feb-18-2020

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


RE: Open an Excel file - jefsummers - Feb-18-2020

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).


RE: Open an Excel file - Friend - Feb-18-2020

(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 :)