mirror of
https://github.com/ChuXunYu/OfficeFileHandle.git
synced 2026-01-31 09:01:25 +00:00
1
This commit is contained in:
@@ -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}")
|
||||
Reference in New Issue
Block a user