Files
OfficeFileHandle/xlsx2csv/xlsx2csv.py
ChuXun c882a7a216 1
2025-12-27 14:36:56 +08:00

22 lines
807 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pandas as pd
import os
# 读取Excel文件获取所有工作表
xlsx_file = "1.xlsx"
# sheet_name=None 会读取所有工作表,返回字典
all_sheets = pd.read_excel(xlsx_file, sheet_name=None)
# 创建输出目录(可选)
output_dir = "csv_output"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 遍历所有工作表分别保存为csv文件
for sheet_name, df in all_sheets.items():
# 处理工作表名称,避免文件名中包含非法字符
safe_name = sheet_name.replace('/', '_').replace('\\', '_').replace(':', '_')
csv_filename = os.path.join(output_dir, f"{safe_name}.csv")
# 保存为CSV自动处理中文编码避免乱码
df.to_csv(csv_filename, index=False, encoding='utf-8-sig')
print(f"已保存: {csv_filename}")