写一串代码让他从几个含有多个表格的Excel文件中按文件顺序提取特定格子的数据并输出成列表形式
时间: 2024-10-16 10:06:47 浏览: 50
首先,你需要安装pandas库,如果你还没有安装,可以使用以下命令安装:
```bash
pip install pandas
```
然后,你可以编写这样的Python代码来实现你的需求:
```python
import os
import pandas as pd
# 定义文件路径、文件列表和目标表格名称
dir_path = "your_directory_path" # 替换为你的文件夹路径
file_names = ['file1.xlsx', 'file2.xlsx', 'file3.xlsx'] # 文件名列表
target_sheet = "Sheet1" # 目标表格名称,可以根据实际情况更改
data_list = []
# 遍历文件和表格
for file_num, filename in enumerate(sorted(file_names)):
full_file_path = os.path.join(dir_path, filename)
excel_df = pd.read_excel(full_file_path, sheet_name=target_sheet) # 读取指定表格
# 提取特定行或列的数据,例如提取第1行和第2列
data_to_extract = excel_df.iloc[[0], [1, 2]] # 这里1表示第一行,可以改为其他行号;2, 3代表列号,替换为实际列号
data_list.append(data_to_extract)
# 输出结果
print("提取的数据列表为:")
for i, df in enumerate(data_list):
print(f"文件{file_num+1} - 表格 {target_sheet}:")
print(df)
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)