Reputation: 91
I am trying to read an Excel file using pandas.read_excel(). The function is only picking up the very first row of the sheet I am passing and it is returning a 0 by 1 dataframe. Not only it just takes the first row, but it sets it as the dataframe label. I tried passing the path as a handle but it did the same result
def read_excel_report(path):
try:
#handle = open(path)
dictionary
# representing a different worksheet.
raw_excel = pd.read_excel(path, sheet_name=-1, engine='openpyxl')
raw_excel = raw_excel.fillna('')
print("data extracted")
return raw_excel
except Exception as e:
print(e)
Upvotes: 0
Views: 1424
Reputation: 153
This happens with the older version of Pandas where Excel files are not created properly.
The best possible solution is to upgrade your Python version to 3.8 (or newer) and Pandas version to 1.2 (or newer). Here is Pandas changelog for your reference.
If that is not possible then the workaround is to open and close the workbook. i.e. use openpyxl
library methods load_workbook
and close
before using read_excel
method of Pandas. This will work but it will add several seconds of delay.
Upvotes: 2