利用Python将文件夹下多个txt文本写入到同一个excel中(每一个文件占一行)-将文件夹下多个EXCEL文本写入到同一个excel中不同的sheet:
最编程
2024-02-23 07:19:03
...
import os
import pandas as pd
# 设置文件夹路径
folder_path = r'G:\Cygwin\SBDART-master1\TestRuns\2000'
# 获取文件夹中所有 Excel 文件的文件名
excel_files = [f for f in os.listdir(folder_path) if f.endswith('.xlsx')]
# 创建一个 ExcelWriter
with pd.ExcelWriter(r'G:\Cygwin\SBDART-master1\TestRuns\2000\2000.xlsx') as writer:
# 遍历每个 Excel 文件并将数据写入到不同 sheet 中
for file in excel_files:
df = pd.read_excel(os.path.join(folder_path, file))
sheet_name = os.path.splitext(file)[0] # 使用文件名作为 sheet 名称
df.to_excel(writer, sheet_name=sheet_name, index=False)