Lukas
Lukas

Reputation: 2312

How to get Excel active sheet name in Python/Pandas?

I'm processing Excel file with multiple sheets via Pandas (read_excel) and need to get the name of the active sheet. Active sheet contains most recent data and the name convention is subject to "creator's mood". So I cannot use simple reading a sheet by name or index... Is there a way how to do it?

UPDATE (solution): Except xlrd with sheet_visible property proposed in Eswar's link (thank you) I discovered another solution with usage of xlwings library:

import xlwings as xw
wb = xw.Book('myfile.xls')
active_sheet_name = wb.sheets.active.name

=> Python is almighty...in many ways ;)

Upvotes: 1

Views: 5404

Answers (1)

Radu
Radu

Reputation: 35

The only solution I see is this:

import pandas as pd

active_sheet = input("Enter the required sheet: ")
df = pd.read_excel(file_with_data, sheet_name = active_sheet)
...

You open the .xlsx file, see what the creator's mood was on that day and how he named the sheet that you need, then simply enter it's name when prompted.

Hope it helps.

Upvotes: 2

Related Questions