mirror of
https://github.com/ChuXunYu/OfficeFileHandle.git
synced 2026-01-31 02:01:26 +00:00
22 lines
807 B
Python
22 lines
807 B
Python
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}") |