<class 'openpyxl.styles.fills.Fill'>
". While I've come across some solutions suggesting manual resaving of the file, this isn't feasible for a large number of files (more than 20,000 in my case). The error "<class 'openpyxl.styles.fills.Fill'>
" in Pandas with Openpyxl typically occurs when there's a compatibility issue between Pandas and the version of Openpyxl you are using. To resolve this error, you can follow these steps:
pip install openpyxl --upgrade
import pandas as pd
print(pd.__version__)
pip install pandas --upgrade
pip install openpyxl==<version>
While browsing through similar queries , we found a potential solution that doesn't require changing library versions or manually modifying files. Here's how we implemented it:
We encountered an error where the .xlsx file raised a TypeError with an expected class of "openpyxl.styles.fills.Fill
" when attempting to open it. To address this, we tried the following approach:
By leveraging xlwings to handle the file opening and saving process, we were able to overcome the TypeError issue and access the data as intended.
If you encounter the error "expected <class 'openpyxl.styles.fills.Fill'>
" while reading an Excel file using Pandas' read_excel
function, you can try the following solution:
read_excel
function. This retry should work without encountering the previous error.Here's an example:
import pandas as pd
import xlwings as xw
# Open the Excel file with xlwings
xl_app = xw.App(visible=False)
wb = xw.Book('excel_file.xlsx')
wb.save('resaved_excel_file.xlsx')
xl_app.quit()
# Retry reading the file with Pandas
df = pd.read_excel('resaved_excel_file.xlsx')
By following this solution, you should be able to overcome the expected <class 'openpyxl.styles.fills.Fill'>
error while reading the Excel file using Pandas.
If you encounter the TypeError "expected <class 'openpyxl.styles.fills.Fill'>
" while attempting to convert multiple .xlsx files to .csv format in Python, you can resolve it by following these steps:
Here's an example code:
import pandas as pd
import xlwings as xw
# List of .xlsx files
xlsx_files = ['file1.xlsx', 'file2.xlsx', 'file3.xlsx']
for file in xlsx_files:
# Open the .xlsx file with xlwings
xl_app = xw.App(visible=False)
wb = xw.Book(file)
wb.save(f'resaved_{file}')
xl_app.quit()
# Retry converting the files to .csv
for file in xlsx_files:
df = pd.read_excel(f'resaved_{file}')
df.to_csv(f'{file[:-5]}.csv', index=False)
By following this solution, you should be able to convert multiple .xlsx files to .csv format without encountering the TypeError related to <class 'openpyxl.styles.fills.Fill'>
.