rikki
rikki

Reputation: 441

How to get sheet no or sheet name of the Active sheet in a Workbook

I am writing a script where I am accessing a worksheet from a Workbook using openpyxl using the code:

wb = load_workbook('Excel.xlsm',read_only=False ,keep_vba=True)
ws = wb.active

Now after updating some of the values in the Excel file I am plotting a graph by reading some portion of the Excel file in Pandas data frame using the code:

hsif=pd.read_excel("Excel.xlsm",sheet_name="K0 Reg Patch Util",skiprows=34)

Now as you can see for reading the Excel sheet in Pandas data frame I am using the sheet name. The problem is I want to give the sheet name as the name of Active sheet in the Workbook. Can somebody please tell me how to get the name of the active sheet or at least sheet no of an active sheet in the Workbook?

I am able to get a reference to active sheet using openpyxl as:

ws = wb.active

But this gives the reference to active sheet not the name of the sheet. I require the name for reading the sheet in pandas data frame or at least the sheet no of the active sheet.

Can somebody tell me how to do that using Pandas or Openpyxl?? Or at least point a way of how to read the active sheet using Pandas?

Upvotes: 0

Views: 5104

Answers (1)

asongtoruin
asongtoruin

Reputation: 10359

The Worksheet object in openpyxl has a .title property - hence, you can simply do:

ws_name = wb.active.title

Upvotes: 2

Related Questions