更新解析excel的函数

原版解析excel函数只能读取sheet1,而且对于合并的单元格只有第一个单元格有数据,其他的为non
This commit is contained in:
Huang ShaoHui 2025-06-09 17:14:39 +08:00 committed by GitHub
parent 7ea1b7bbe8
commit ab4d2da1cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 13 deletions

View File

@ -1,20 +1,32 @@
import pandas as pd import pandas as pd
def parse_excel(file_path): def parse_excel(file_path):
# 读取Excel文件 # 读取所有工作表
df = pd.read_excel(file_path) all_sheets = pd.read_excel(file_path, sheet_name=None) # 读取所有sheet
# 获取表头
headers = df.columns.tolist()
blocks = []
for _, row in df.iterrows():
# 构建HTML表格
html_table = "<html><body><table><tr>{}</tr><tr>{}</tr></table></body></html>".format("".join(f"<td>{col}</td>" for col in headers), "".join(f"<td>{row[col]}</td>" for col in headers))
block = {"type": "table", "img_path": "", "table_caption": [], "table_footnote": [], "table_body": f"{html_table}", "page_idx": 0}
blocks.append(block)
blocks = []
for sheet_name, df in all_sheets.items():
df = df.fillna(method='ffill')#填充合并的单元格
headers = df.columns.tolist()
for _, row in df.iterrows():
html_table = "<html><body><table><tr>{}</tr><tr>{}</tr></table></body></html>".format(
"".join(f"<td>{col}</td>" for col in headers),
"".join(f"<td>{row[col]}</td>" for col in headers)
)
print(row['测试分类'])
block = {
"type": "table",
"img_path": "",
"table_caption": [f"Sheet: {sheet_name}"],
"table_footnote": [],
"table_body": f"{html_table}",
"page_idx": 0
}
blocks.append(block)
return blocks return blocks