首页 > 解决方案 > 根据工作表标题从许多 Excel 工作簿中提取工作表

问题描述

我需要从许多 Excel 工作簿中提取特定工作表。工作表在每个 Excel 工作簿中的标题完全相同。

提取后,我需要根据相应 Excel 工作簿标题的开头命名每个数据框(从每个提取的工作表创建)。 示例:对于标题为“ Pizza ”的工作表(每个 Excel 工作簿都相同)和标题为“ Coke_2021 ”的 Excel 工作簿,数据框应自动命名为“ Pizza_Coke ”。Excel 工作簿的格式为:' Coke_2021 '、' Sprite_2019 '等,因此非常可预测。

我有以下代码,但卡在第 1 步(提取工作表)。

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pyodbc
import openpyxl
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl import load_workbook 
from openpyxl.worksheet.table import Table
import xlwings as xw
import pandas as pd
import datetime
import numpy as np
import itertools
import ntpath
import calendar

## UI - Asking user for their input and output files
root = tk.Tk()
root.withdraw()
root.databases =  filedialog.askopenfilenames(initialdir = "C:/",title = "Select the location of your Soda files (you may select multiple files)", filetypes = (("Excel Files","*.xlsx"),("all files","*.*")))
db_list = root.tk.splitlist(root.databases)

标签: pythonexcelpandasdatabaseextract

解决方案


我不太清楚命名每个数据框是什么意思,但我们可以加载数据框并生成所需的名称。

Pandas 在阅读 Excel 和 CSV 文件时非常有用。该pandas.read_excel()函数可以将特定的 Excel 工作表读入数据框,也可以将所有工作表作为数据框字典读入。有关详细信息,请参阅:https ://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html?highlight= read_excel。

下面的代码从 Excel 文件列表中加载“Pizza”表,并将它们存储为dataframes具有“sheet_file”命名的字典。

import pandas as pd

sheet_name = 'Pizza'
xl_files = ['Coke_2021.xlsx', 'Sprite_2019.xlsx']

dataframes = {}
for xl_file in xl_files:
    # create new name with sheet name & first part of file name
    name = f'{sheet_name}_{xl_file.split("_")[0]}'
    dataframes[name] = pd.read_excel(xl_file, sheet_name=sheet_name)

# now can access a given dataframe by the "sheet_file" naming
# could also easily export to Excel with the new name like below
dataframes['Pizza_Coke'].to_excel('Pizza_Coke.xlsx')

上面,我们假设您有可用的 Excel 文件名,例如分配给xl_files. 从您的代码中,看起来可能存在存储在db_list使用绝对路径的 Excel 文件的列表。在这种情况下,我们需要稍微修改名称创建代码以仅使用文件名而不是整个路径。这样的事情应该会有所帮助:

import os

dataframes = {}
for xl_file in db_list:
    filename = os.path.basename(xl_file)  # use this in next line!
    name = f'{sheet_name}_{filename.split("_")[0]}'
    dataframes[name] = pd.read_excel(xl_file, sheet_name=sheet_name)

相反,如果您想从一个 Excel 文件加载多张工作表,则以下代码会生成相同类型的结果。

import pandas as pd

xl_file = 'Coke_2021.xlsx'

# load the Excel file's sheets into a dictionary
# of form: {sheet_name: sheet_dataframe, ...}
dataframes = pd.read_excel(xl_file, None)

renamed_dataframes = {}
for sheet_name, df in dataframes.items():
    new_name = f'{sheet_name}_{xl_file.split("_")[0]}'
    renamed_dataframes[new_name] = df

renamed_dataframes['Pizza_Coke'].to_excel('Pizza_Coke.xlsx')

推荐阅读