读取excel表格内容

import pandas as pd


def get_excel_data(file_path):
try:
# 读取 Excel 文件
df = pd.read_excel(file_path)

# 获取表头
headers = df.columns.tolist()

# 获取内容并转换为列表的字典,每一行对应一个字典
content = df.to_dict(orient='records')

return {'headers': headers, 'content': content}
except Exception as e:
print(f"读取文件时发生错误: {e}")
return None


if __name__ == "__main__":
file_path = 'example.xls'
result = get_excel_data(file_path)
if result is not None:
print("表头:", result['headers'])
print("\n内容:")
for row in result['content']:
print(row)