python3 读取当前目录下所有的excel文件读取所有的sheet页按行、列读取所有的单元格内容判断是否等于X001的值取出当前行的所有列内容
以下是一个示例代码,可以实现读取当前目录下所有的excel文件,读取所有的sheet页,按行、列读取所有的单元格内容,并判断是否等于"X001"的值,如果是则取出当前行的所有列内容。
import os
import pandas as pd
# 获取当前目录下所有的excel文件
files = [file for file in os.listdir(".") if file.endswith(".xlsx") or file.endswith(".xls")]
for file in files:
print("正在处理文件:", file)
# 读取excel文件
excel = pd.ExcelFile(file)
for sheet_name in excel.sheet_names:
print("正在处理sheet页:", sheet_name)
# 读取sheet页数据
df = pd.read_excel(excel, sheet_name)
# 遍历每一行
for index, row in df.iterrows():
# 遍历每一列
for col in df.columns:
cell_value = row[col]
if cell_value == "X001":
print("找到匹配值在第", index + 1, "行,列名为:", col)
print("当前行的所有列内容:", row.tolist())
请注意,以上代码使用了pandas库来读取excel文件,因此需要先安装pandas库。可以使用以下命令进行安装:
pip install pandas
原文地址: https://www.cveoy.top/t/topic/i2zN 著作权归作者所有。请勿转载和采集!