Solve the error problem when using pd.read_excel in Python

Solve the error problem when using pd.read_excel in Python

In Python, we often need to read Excel table files for data analysis and processing. Among them, the read_excel function of the pandas library is a very commonly used method. It can directly read Excel tables and convert them into DataFrame format, which is very convenient. However, when using the read_excel function, the error xlrd.biffh.XLRDError: Excel xlsx file not supported sometimes occurs, preventing us from reading the Excel table normally. So, how to solve this problem?

The reason for this error is that our code uses the xlrd library to read Excel files in xlsx format, and the xlrd library does not support the xlsx format. Therefore, we need to install a library that supports xlsx format-openpyxl.

The specific steps are as follows:

  1. First, we need to make sure that the openpyxl library is installed. If it is not installed, you can install it through the pip install openpyxl command.

  2. Next, before using the read_excel function of the pandas library, you need to call the ExcelFile method of the pandas library and set the parameter engine to 'openpyxl', which means using the openpyxl library to read the Excel file. The sample code is as follows:

import pandas as pd

file_path = 'example.xlsx'

excel_file 

Guess you like

Origin blog.csdn.net/update7/article/details/131566591