I'm looking to create a Pandas data frame by asking the user to choose a file from their directory. In addition to choosing the file, though, I'm looking to also specify a specific sheet in the file. If only one sheet exists, then automatically choose the first one.
I have tried the code below:
import pandas as pd
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
df = pd.read_excel(file_path, sheet_name = 1)
df
This works, but it doesn't provide the user the ability to choose which sheet of the excel file they'd like to use. Perhaps a new window should pop up (if there's more than one sheet) and ask the user to select which sheet names are available. If only one sheet is available, then it should automatically choose the first sheet.