python 遍历文件夹EXCEL 并按第1列拆分工作表
时间: 2024-03-12 11:46:55 浏览: 89
好的,您可以参考以下代码实现:
```python
import os
import pandas as pd
# 待遍历的文件夹路径
folder_path = "your/folder/path"
# 遍历文件夹
for root, dirs, files in os.walk(folder_path):
for file in files:
# 判断是否为 EXCEL 文件
if file.endswith(".xlsx") or file.endswith(".xls"):
# 读取 EXCEL 文件
file_path = os.path.join(root, file)
df = pd.read_excel(file_path)
# 按第一列拆分工作表
for name, group in df.groupby(df.columns[0]):
# 生成新的 EXCEL 文件名
new_file_name = f"{os.path.splitext(file)[0]}_{name}.xlsx"
new_file_path = os.path.join(root, new_file_name)
# 写入新的 EXCEL 文件
group.to_excel(new_file_path, index=False)
```
上述代码会遍历指定文件夹,查找所有的 EXCEL 文件,并按照每个文件的第一列进行拆分,生成新的工作表并保存至原文件夹下。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)