使用正则表达式在Excel电子表格中循环浏览选项卡名称

时间:2019-04-19 21:27:22

标签: python regex excel

我试图遍历一些Excel电子表格,并使用正则表达式从某些选项卡中提取信息。到目前为止,我已经编写了以下代码:

import pandas as pd
import os
import re

root = r"my_dir"

agg_df = pd.DataFrame()

for directory, subdirectory, files in os.walk(root):
    for file in files:
        if file.endswith('.xlsm'):
            filepath = os.path.join(directory, file)
            xls = pd.ExcelFile(filepath)
            for i in xls.sheet_names:
                if re.search(r'Apples', i):
                    df_temp = pd.read_excel(filepath, sheet_names=i)
                    df_temp['Filepath'] = filepath
                    df_temp['Sheet_Name'] = i
                    agg_df = agg_df.append(df_temp)
                elif re.search(r'Oranges', i):
                    df_temp = pd.read_excel(filepath, sheet_names=i)
                    df_temp['Filepath'] = filepath
                    df_temp['Sheet_Name'] = i
                    agg_df = agg_df.append(df_temp)
                elif re.search('Grapes', i):
                    df_temp = pd.read_excel(filepath, sheet_names=i)
                    df_temp['Filepath'] = filepath
                    df_temp['Sheet_Name'] = i
                    agg_df = agg_df.append(df_temp)
                elif re.search(r'Tomatoes', i):
                    df_temp = pd.read_excel(filepath, sheet_names=i)
                    df_temp['Filepath'] = filepath
                    df_temp['Sheet_Name'] = i
                    agg_df = agg_df.append(df_temp)
                elif re.search(r'Peaches', i):
                    df_temp = pd.read_excel(filepath, sheet_names=i)
                    df_temp['Filepath'] = filepath
                    df_temp['Sheet_Name'] = i
                    agg_df = agg_df.append(df_temp)
                elif re.search(r'Pears', i):
                    df_temp = pd.read_excel(filepath, sheet_names=i)
                    df_temp['Filepath'] = filepath
                    df_temp['Sheet_Name'] = i
                    agg_df = agg_df.append(df_temp)
                elif re.search(r'Bananas', i):
                    df_temp = pd.read_excel(filepath, sheet_names=i)
                    df_temp['Filepath'] = filepath
                    df_temp['Sheet_Name'] = i
                    agg_df = agg_df.append(df_temp)
                elif re.search(r'Mangos', i):
                    df_temp = pd.read_excel(filepath, sheet_names=i)
                    df_temp['Filepath'] = filepath
                    df_temp['Sheet_Name'] = i
                    agg_df = agg_df.append(df_temp) 

但是,这是拉第一个标签,而不是我要指定的标签。

1 个答案:

答案 0 :(得分:0)

您正在传递参数 sheet_names ,但是pandas文档中提到的参数是 sheet_name 。请参见read_excel()函数。

我对您的代码进行了一些更改,请尝试执行:

import os

import pandas as pd


def parse_excel(sheet_name: str, abs_file_path: str):
    df = pd.read_excel(abs_file_path, sheet_name=sheet_name)
    df['Filepath'] = abs_file_path
    df['Sheet_Name'] = sheet_name
    return df


root = "my_dir"
agg_df = pd.DataFrame()
for directory, subdirectory, files in os.walk(root):
    for file in files:
        if file.endswith('.xlsm'):
            file_path = os.path.join(directory, file)
            xls = pd.ExcelFile(file_path)
            sheet_names = ['Apples', 'Oranges', 'Grapes', 'Tomatoes', 'Peaches',
                           'Pears', 'Bananas', 'Mangos']
            available_sheets = [sheet_name for sheet_name in xls.sheet_names if sheet_name in sheet_names]
            for available_sheet in available_sheets:
                agg_df = agg_df.append(parse_excel(available_sheet, file_path))