I am trying to read an xls file from a url into python dataframe. However I am getting below assertion error.
Traceback (most recent call last):
File "c:\Sample_project\venv\excel_read_v1.py", line 17, in
df = pd.read_excel("file.xls",
File "C:\Sample_project\venv\lib\site-packages\pandas\util_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:\Sample_project\venv\lib\site-packages\pandas\io\excel_base.py", line 457, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File "C:\Sample_project\venv\lib\site-packages\pandas\io\excel_base.py", line 1419, in init
self._reader = self._engines[engine](self.io, storage_options=storage_options)
File "C:\Sample_project\venv\lib\site-packages\pandas\io\excel_xlrd.py", line 25, in init
super().init(filepath_or_buffer, storage_options=storage_options)
File "C:\Sample_project\venv\lib\site-packages\pandas\io\excel_base.py", line 518, in init
self.book = self.load_workbook(self.handles.handle)
File "C:\Sample_project\venv\lib\site-packages\pandas\io\excel_xlrd.py", line 38, in load_workbook
return open_workbook(file_contents=data)
File "C:\Sample_project\venv\lib\site-packages\xlrd_init.py", line 172, in open_workbook
bk = open_workbook_xls(
File "C:\Sample_project\venv\lib\site-packages\xlrd\book.py", line 104, in open_workbook_xls
bk.parse_globals()
File "C:\Sample_project\venv\lib\site-packages\xlrd\book.py", line 1211, in parse_globals
self.handle_sst(data)
File "C:\Sample_project\venv\lib\site-packages\xlrd\book.py", line 1178, in handle_sst
self._sharedstrings, rt_runlist = unpack_SST_table(strlist, uniquestrings)
File "C:\Sample_project\venv\lib\site-packages\xlrd\book.py", line 1472, in unpack_SST_table
assert _unused_i == nstrings - 1
AssertionError
I read some other suggestions on stackoverflow that if I remove the last few empty lines from the excel then it would work. So I tried that out by downloading the file in a local folder , removing the last 2 empty rows and then reading the file from the local folder, this works. But i need the code to somehow able to handle it while reading from the url so that we can automate the process
I have tried using openpyxl and xlrd to read the file.
---Code snapshot below--------
import openpyxl
import xlrd
from xlrd import open_workbook
import requests
import pandas as pd
url = url
r = requests.get(url)
with open('maskefile.xls', 'wb') as output:
output.write(r.content)
df = pd.read_excel("maskedfile.xls",sheet_name = "maskedsheetname")
df.to_csv("C:\Sample_project\maskedfile.csv" ,index = False)
Related
I have a folder with many xlsx files that I'd like to convert to csv files.
During my research, if found several threads about this topic, such as this or that one. Based on this, I formulated the following code using glob and pandas:
import glob
import pandas as pd
path = r'/Users/.../xlsx files'
excel_files = glob.glob(path + '/*.xlsx')
for excel in excel_files:
out = excel.split('.')[0]+'.csv'
df = pd.read_excel(excel) # error occurs here
df.to_csv(out)
But unfortunately, I got the following error message that I could not interpret in this context and I could not figure out how to solve this problem:
Traceback (most recent call last):
File "<input>", line 11, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/util/_decorators.py", line 299, in wrapper
return func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_base.py", line 336, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_base.py", line 1131, in __init__
self._reader = self._engines[engine](self._io, storage_options=storage_options)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_openpyxl.py", line 475, in __init__
super().__init__(filepath_or_buffer, storage_options=storage_options)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_base.py", line 391, in __init__
self.book = self.load_workbook(self.handles.handle)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/excel/_openpyxl.py", line 486, in load_workbook
return load_workbook(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 317, in load_workbook
reader.read()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 281, in read
apply_stylesheet(self.archive, self.wb)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/styles/stylesheet.py", line 198, in apply_stylesheet
stylesheet = Stylesheet.from_tree(node)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/styles/stylesheet.py", line 103, in from_tree
return super(Stylesheet, cls).from_tree(node)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/descriptors/serialisable.py", line 87, in from_tree
obj = desc.expected_type.from_tree(el)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/descriptors/serialisable.py", line 87, in from_tree
obj = desc.expected_type.from_tree(el)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openpyxl/descriptors/serialisable.py", line 103, in from_tree
return cls(**attrib)
TypeError: __init__() got an unexpected keyword argument 'xfid'
Does anyone know how to fix this? Thanks a lot for your help!
I had the same problem here. After some hours thinking and searching I realized the problem is, actually, the file. I opened it using MS Excel, and save. Alakazan, problem solved.
The file was downloaded, so i think it's a "security" error or just an error from how the file was created. xD
EDIT:
It's not a security problem, but actually an error from the generation of file. The correct has the double of kb the wrong file.
An solution is: if using xlrd==1.2.0 the file can be opened, you can, after doing this, call read_excel to the Book(file opened by xlrd).
import xlrd
# df = pd.read_excel('TabelaPrecos.xlsx')
# The line above is the same result
a = xlrd.open_workbook('TabelaPrecos.xlsx')
b = pd.read_excel(a)
I'm a new in Docker, and I need to run a Flask app, but in this app, I need to read a xlsx file.
When I build my docker image, I have the excel file but Docker don't read it.
See the part of my code with the xlsx :
try :
# Page PayPlug
infos_pay = pd.read_excel('./app-infos.xlsx', sheet_name='payplug')
secret_key = infos_pay.loc[0].tolist()
payplug.set_secret_key(secret_key[0])
# Page mail
feuille_mail = pd.read_excel('./app-infos.xlsx', sheet_name='mail')
infos_mail = feuille_mail.loc[0].tolist()
except :
print("Can't read file")
And my docker file :
FROM python:3.8
WORKDIR /code
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD [ "python", "./web_AuSpot.py" ]
When I use ls cmd in the cli of the image, I see the xlsx file, so why pandas can't read it ?
Thank you, I hope my explanation is clear.
Reading excel files isn't necessary to pandas operation. So you need to also list the xlrd package in your requirements file. If that doesn't fix it you will need to list out the requirements.txt file and also add the docker error message would be helpful to put into your question.
Other possibilities are:
Could be a file location problem.
could be that pandas wasn't imported as pd (I don't see it in the code)
I remove the try expect and I have this error :
Traceback (most recent call last):
File "./web_AuSpot.py", line 46, in <module>
infos_pay = pd.read_excel('/app-infos.xlsx', sheet_name='payplug')
File "/usr/local/lib/python3.8/site-packages/pandas/util/_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "/usr/local/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 867, in __init__
self._reader = self._engines[engine](self._io)
File "/usr/local/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py", line 21, in __init__
import_optional_dependency("xlrd", extra=err_msg)
File "/usr/local/lib/python3.8/site-packages/pandas/compat/_optional.py", line 110, 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.
(I can't put it in a comment so I put it in)
EDIT :
I add the xrld in requirements, change the location path of the xlsx, and now I have this :
Traceback (most recent call last):
File "./web_AuSpot.py", line 46, in <module>
infos_pay = pd.read_excel('app-infos.xlsx', sheet_name='payplug')
File "/usr/local/lib/python3.8/site-packages/pandas/util/_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "/usr/local/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 867, in __init__
self._reader = self._engines[engine](self._io)
File "/usr/local/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py", line 22, in __init__
super().__init__(filepath_or_buffer)
File "/usr/local/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 353, in __init__
self.book = self.load_workbook(filepath_or_buffer)
File "/usr/local/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py", line 37, in load_workbook
return open_workbook(filepath_or_buffer)
File "/usr/local/lib/python3.8/site-packages/xlrd/__init__.py", line 170, in open_workbook
raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
xlrd.biffh.XLRDError: Excel xlsx file; not supported
I try to read a xlsx into a data frame:
itut_ir = pd.read_excel('C:\\Users\\Administrator\\Downloads\\reportdata.xlsx')
print(itut_ir.to_string())
I receive this:
Traceback (most recent call last):
File
"C:\Users\Administrator\eclipse-workspace\Reports\GOW\Report.py",
line 44, in
df = pd.read_excel('C:\Users\Administrator\Downloads\reportdata.xlsx')
File
"C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel_base.py",
line 304, in read_excel
io = ExcelFile(io, engine=engine) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel_base.py",
line 824, in init
self._reader = self.enginesengine File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel_xlrd.py",
line 21, in init
super().init(filepath_or_buffer) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel_base.py",
line 353, in init
self.book = self.load_workbook(filepath_or_buffer) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel_xlrd.py",
line 36, in load_workbook
return open_workbook(filepath_or_buffer) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd_init.py",
line 117, in open_workbook
zf = zipfile.ZipFile(filename) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\zipfile.py",
line 1222, in init
self._RealGetContents() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\zipfile.py",
line 1289, in _RealGetContents
raise BadZipFile("File is not a zip file") zipfile.BadZipFile: File is not a zip file
does anybody have an idea? the file does not seem to be broken, I can open it with Excel.
thanks!
*** UPDATE ***
the file producing the error is being downloaded from FTP. opening the original file works ... if that gives you a hint :) thanks
I had the same issue just a little bit ago with an XLSX that I created in LibreOffice.
The solution was to check the XLSX to make sure it wasn't corrupted. In my case, loading a previous version of the XLSX file corrected the problem.
I am working on a personal project to analyze COVID19 data. Presently, I am download the excel sheet provided by ourworldindata.org, available at this url -> https://github.com/owid/covid-19-data/blob/master/public/data/owid-covid-data.xlsx
However, when i try to execute the command in pandas (below), I get a list of errors. What could be the root cause ?
url = 'https://github.com/owid/covid-19-data/blob/master/public/data/owid-covid-data.xlsx'
df = pd.read_excel(url, sheet_name='Sheet1')
Error
Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Users\masoom.kumar\PycharmProjects\ReadingINCA_Data\venv\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine) File "C:\Users\masoom.kumar\PycharmProjects\ReadingINCA_Data\venv\lib\site-packages\pandas\io\excel\_base.py", line 824, in __init__
self._reader = self._engines[engine](self._io) File "C:\Users\masoom.kumar\PycharmProjects\ReadingINCA_Data\venv\lib\site-packages\pandas\io\excel\_xlrd.py", line 21, in __init__
super().__init__(filepath_or_buffer) File "C:\Users\masoom.kumar\PycharmProjects\ReadingINCA_Data\venv\lib\site-packages\pandas\io\excel\_base.py", line 351, in __init__
self.book = self.load_workbook(filepath_or_buffer) File "C:\Users\masoom.kumar\PycharmProjects\ReadingINCA_Data\venv\lib\site-packages\pandas\io\excel\_xlrd.py", line 34, in load_workbook
return open_workbook(file_contents=data) File "C:\Users\masoom.kumar\PycharmProjects\ReadingINCA_Data\venv\lib\site-packages\xlrd\__init__.py", line 157, in open_workbook
ragged_rows=ragged_rows, File "C:\Users\masoom.kumar\PycharmProjects\ReadingINCA_Data\venv\lib\site-packages\xlrd\book.py", line 92, in open_workbook_xls
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS) File "C:\Users\masoom.kumar\PycharmProjects\ReadingINCA_Data\venv\lib\site-packages\xlrd\book.py", line 1278, in getbof
bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8]) File "C:\Users\masoom.kumar\PycharmProjects\ReadingINCA_Data\venv\lib\site-packages\xlrd\book.py", line 1272, in bof_error
raise XLRDError('Unsupported format, or corrupt file: ' + msg) xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\n\n\n\n\n<!D'
Please not that pandas can read the excel if I download it on my computer
Try the link to raw excel file:
import pandas as pd
url='https://github.com/owid/covid-19-data/blob/master/public/data/owid-covid-data.xlsx?raw=true'
df=pd.read_excel(url, sheet_name='Sheet1')
You can do it with requests
import pandas as pd
import io
import requests
url = 'https://github.com/owid/covid-19-data/blob/master/public/data/owid-covid-data.xlsx'
get_content = requests.get(url).content
df = pd.read_csv(io.StringIO(get_content .decode('utf-8')))
I do this to avoid using local drive or google drive , and saves time of connection.
I'm attempting to open an xlsx-file using load_workbook from the module openpyxl. The code I have is:
import os
from openpyxl import load_workbook
def edit_workbook():
path = r'C:\123 ABC\Excel documents'
filename = 'filename.xlsx'
os.path.join(path, filename)
workbook = load_workbook(os.path.join(path, filename))
## Error is on the line above.
The complete error message I get is:
Traceback (most recent call last):
File "<ipython-input-12-22dfdfc4e5e1>", line 1, in <module>
workbook = load_workbook(os.path.join(path, filename))
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\reader\excel.py", line 214, in load_workbook
apply_stylesheet(archive, wb) # bind styles to workbook
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\styles\stylesheet.py", line 176, in apply_stylesheet
stylesheet = Stylesheet.from_tree(node)
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\styles\stylesheet.py", line 99, in from_tree
return super(Stylesheet, cls).from_tree(node)
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 79, in from_tree
obj = desc.expected_type.from_tree(el)
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 79, in from_tree
obj = desc.expected_type.from_tree(el)
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 79, in from_tree
obj = desc.expected_type.from_tree(el)
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 92, in from_tree
return cls(**attrib)
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\styles\table.py", line 37, in __init__
self.dxfId = dxfId
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\base.py", line 69, in __set__
value = _convert(self.expected_type, value)
File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\base.py", line 59, in _convert
raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'int'>
Anyone know what this can be?
I received the same error. In my case, the Excel workbook has no charts, no filters, no formulas, no VBA. Only data. This workbook was generated by some third party software.
Turns out that the workbook was corrupted. I found that out when trying to save it after some very minor change. Once I resolved the corruption (by letting Excel save it to a different name as it requested), the openpyxl error disappeared.