1、将xls批量转换成为csv
import pandas as pd
import os
import glob# 设置文件路径
input_folder = r"D:\Lenovo\桌面\小学工作内容\25下" # 替换为你的XLS文件所在文件夹
output_folder = r"E:\OldComputer\python学习\python实战\output" # 替换为你想保存CSV文件的文件夹# 获取所有XLS文件
xls_files = glob.glob(os.path.join(input_folder, "*.xls"))for xls_file in xls_files:try:# 读取XLS文件df = pd.read_excel(xls_file)# 生成输出文件名(保持原文件名,只更改扩展名)base_name = os.path.basename(xls_file)csv_name = os.path.splitext(base_name)[0] + ".csv"output_path = os.path.join(output_folder, csv_name)# 保存为CSVdf.to_csv(output_path, index=False, encoding='ANSI')print(f"已转换: {base_name} -> {csv_name}")except Exception as e:print(f"转换失败: {xls_file}, 错误: {str(e)}")print("批量转换完成!")