以下代码请求用户选择一个他们想要导入为 pandas 数据框的 Excel 文件;但是,它不提供选择哪个工作表的功能(如果存在多个工作表):
import pandas as pd
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
path = filedialog.askopenfilename()
x = pd.read_excel(path, sheet_name = 1)
x
新解决方案中包含的条件:
- 如果只有一张表,则自动选择并上传到 pandas 数据框
- 如果存在多个工作表,允许用户通过对话框选择要导入的工作表
最佳答案
@GordonAitchJay 提供的解决方案以及使用 Tkinter 的实现非常出色。如果您直接使用 Python 或在 Spyder 等 IDE 中运行脚本,这绝对是最佳选择。
然而,OP正在Jupyter中工作,结果发现Jupyter和Tkinter相处得不太好。 OP 表达了困难,虽然我一开始确实让它工作,但如果我插入代码的性能,我也注意到严重的滞后和问题。既然如此,我想我只需添加一种方法,通过使用 ipywidgets 使交互在 Jupyter 中顺利进行。框架。
# Jupyter notebook
import pandas as pd
import ipywidgets as widgets
from IPython.display import clear_output
from ipyfilechooser import FileChooser
from ipywidgets import interact
from pathlib import Path
# get home dir of user
home = str(Path.home())
# initialize a dict for the excel file; this removes the need to set global values
dict_file = {}
# change to simply `home` if you want users to navigate through diff dirs
fc = FileChooser(f'{home}/excel')
# same here
fc.sandbox_path = f'{home}/excel'
# limit file extensions to '.xls, .xlsb, .xlsm, .xlsx'
fc.filter_pattern = ['*.xls*']
fc.title = '<b>Select Excel file</b>'
display(fc)
# create empty dropdown for sheet names
dropdown = widgets.Dropdown(options=[''], value='', description='Sheets:', disabled=False)
# create output frame for the df
out = widgets.Output(layout=widgets.Layout(display='flex', flex_flow='column', align_items='flex-start', width='100%'))
# callback func for FileChooser
def get_sheets(chooser):
# (re)populate dict
dict_file.clear()
dict_file['file'] = pd.ExcelFile(fc.value)
sheet_names = dict_file['file'].sheet_names
# only 1 sheet, we'll print this one immediate (further below)
if len(sheet_names) == 1:
# set value of the dropdown to this sheet
dropdown.options = sheet_names
dropdown.value = sheet_names[0]
# disable the dropdown; so it's just showing the selection to the user
dropdown.disabled = True
else:
# append empty string and set this as default; this way the user must always make a deliberate choice
sheet_names.append('')
dropdown.options = sheet_names
dropdown.value = sheet_names[-1]
# allow selection by user
dropdown.disabled = False
return
# bind FileChooser to callback
fc.register_callback(get_sheets)
# prompt on selection sheet
def show_df(sheet):
if sheet == '':
if out != None:
# clear previous df, when user selects a new wb
out.clear_output()
else:
# clear previous output 'out' frame before displaying new df, else they'll get stacked
out.clear_output()
with out:
df = dict_file['file'].parse(sheet_name=sheet)
if len(df) == 0:
# if sheet is empty, let the user know
display('empty sheet')
else:
display(df)
return
# func show_df is called with input of widget as param on selection sheet
interact(show_df, sheet=dropdown)
# display 'out' (with df)
display(out)
笔记本中的交互片段:
关于python - 从 Python 导入的 Excel 文件中选择特定工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72605306/