使用python,选择文件夹,将文件夹内所有个工作簿内的所有工作表合并至一个新工作簿中,并将新工作簿保存至桌面
时间: 2023-05-15 17:06:43 浏览: 94
你可以使用Python中的pandas库来完成这个任务。以下是代码示例:
```python
import os
import pandas as pd
# 选择文件夹
folder_path = input("请输入文件夹路径:")
# 遍历文件夹内所有Excel文件
excel_files = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".xlsx") or file.endswith(".xls"):
excel_files.append(os.path.join(root, file))
# 合并所有工作表
all_data = pd.DataFrame()
for file in excel_files:
excel_data = pd.read_excel(file, sheet_name=None)
for sheet_name, sheet_data in excel_data.items():
all_data = all_data.append(sheet_data)
# 保存至桌面
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
save_path = os.path.join(desktop_path, "merged.xlsx")
all_data.to_excel(save_path, index=False)
print("合并完成,文件保存至桌面!")
```
你可以将以上代码复制到Python文件中运行,然后输入文件夹路径即可完成合并。
阅读全文
相关推荐

















