This commit is contained in:
ChuXun
2025-12-27 14:36:56 +08:00
parent 95028f8070
commit c882a7a216
11 changed files with 2455 additions and 4 deletions

View File

@@ -1,4 +1,22 @@
import pandas as pd
# 自动处理了中文编码,避免乱码
df = pd.read_excel("1.xlsx")
print(df.to_csv(index=False))
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}")