Windows系统python批量读取不同的excel文件合成一个文件
时间: 2023-07-18 07:16:13 浏览: 110
您可以使用Python中的pandas库来读取和合并不同的Excel文件。以下是一个示例代码,它假定您的Excel文件都位于同一个文件夹中:
```python
import os
import pandas as pd
# 设置文件夹路径
folder_path = 'path/to/folder/containing/excel/files'
# 获取文件夹中所有Excel文件的文件名
file_names = [f for f in os.listdir(folder_path) if f.endswith('.xlsx')]
# 创建一个空DataFrame以存储合并后的数据
merged_data = pd.DataFrame()
# 循环遍历所有Excel文件并将它们合并到一个DataFrame中
for file_name in file_names:
file_path = os.path.join(folder_path, file_name)
data = pd.read_excel(file_path)
merged_data = pd.concat([merged_data, data], ignore_index=True)
# 将合并后的数据保存到一个新的Excel文件中
merged_data.to_excel('path/to/output/file.xlsx', index=False)
```
您需要将`folder_path`替换为您实际的文件夹路径,并将`'path/to/output/file.xlsx'`替换为您希望保存合并后数据的文件路径。此外,您可能需要更改`read_excel`函数的参数以匹配您Excel文件的实际格式。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)