Obtain name of worksheet using openpyxl - python

I am using openpyxl to access all of the tabs in a spreadsheet using the following:
rawReturnwb = openpyxl.load_workbook(ValidationsDir)
for sheet in rawReturnwb.worksheets:
do something...
This works fine. I then would like to access the worksheet name to use else where in my code. However when I try access the worksheet name (printing sheet to the console) I get:
<Worksheet "SheetName">
the type of sheet is
<class 'openpyxl.worksheet.worksheet.Worksheet'>
Is there a way that I can just get the worksheet name returned (so my output would be "SheetName" only. Or would I have to convert to string and strip the parts of the string I don't need?

As suggested in comment of the question, sheet.title is working.
For example, this is some code to get the Worksheet name from a given cell:
from openpyxl.cell import Cell
def get_cell_name(cell:Cell) -> str:
"""Get the name of the Worksheet of a given cell"""
return cell.parent.title
And in the case of the OP, the code could be something like:
rawReturnwb = openpyxl.load_workbook(ValidationsDir)
for sheet in rawReturnwb.worksheets:
# ...
if sheet.title == "Sheet1":
continue
# ...
FYI, the names of the variables are weird:
They should be lowercase in Python (according to PEP-8)
ValdiationsDir is weird, for the path of an Excel File
rawReturnwb could be renamed to book for example

Related

How to get the CodeName of a sheet

I'm trying to use xlwings to deal with Excel files similarlly to what I used to do via VBA.
As I've learned so far, I can access a spreadsheet using name or index. but both of which can be modified. Is there a way to access a sheet using the codename?
Here is an example:
I have a workbook with 3 sheets inside. one of it is a special sheet that I've modified its CodeName in VBA editor to shReport. So no matter who uses this file and rename the sheet to "Report" or "NiceReport", in VBA I can always use shReport.cells(1,1) to get what I need.
But in xlwings, I can only (seems to be) use sht = wb.Sheets['Report'] or sht = wb.Sheets[0] to get the sheet as object. this will fail if user rename the sheet or inseart or delete sheets which will change the index.
So I wonder if it's possible to use the CodeName to refer to the sheet. I've tried in api and don't get any return of CodeName. the code below will return nothing
for sht in wk.Sheets:
print(sht.api.CodeName)
Not sure why your code does not work, however this complete code example works
import xlwings as xw
wb = xw.Book('Book1.xlsx')
for sheet in wb.sheets:
print("Sheet Name: " + str(sheet))
print("Code Name: " + str(wb.sheets(sheet).api.CodeName))
The last line can be split into
sht = wb.sheets(sheet)
print("Code Name: " + str(sht.api.CodeName))
so its the same as your line of code.
From what I can see you would only get the Code Name from a selected sheet so have to check each sheet for the one that matches as you did.

hyperlink in pandas (dataframe to excel)

I'm trying to create a excel file with url link in a specific column.
Like this
def fill_table(table):
if elapsed_time > datetime.timedelta(minutes=args.mtime):
table.loc[len(table)] = [hostname, int(trigger_id), description, eventStartDate, eventEndDate, elapsed_time, message, useralias, link]
...
writer = pd.ExcelWriter(args.output, engine='xlsxwriter')
I tried to use the excel hyperlink formula in the link variable
link = '=HYPERLINK(\"{0}/tr_events.php?triggerid={1}&eventid={2}\"; \"{3}\")'.format(args.url, trigger_id,event['eventid'], event['name'])
But I get a error message when open the file and the column 'link' fill with zero's
Probably you need a comma (,) instead of a semi-colon (;) in the formula. This is because Excel stores formulas in US-style syntax (see Non US Excel functions and syntax in the XlsxWriter docs).
When I run your formula through XlsxWriter I get an Excel warning about "We found a problem with some content in 'demo.xlsx'" and when I click on "yes" to recover the formula is zero, as your described.
Changing the semi-colon to a comma makes the program work without warning and as expected:
import xlsxwriter
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
link = '=HYPERLINK(\"{0}/tr_events.php?triggerid={1}&eventid={2}\", \"{3}\")'.format('www.foo.com', 'abc', 'def', 'event1')
worksheet.write('A1', link)
# Or with a hyperlink format.
url_format = workbook.get_default_url_format()
worksheet.write('A2', link, url_format)
workbook.close()
Output:
Use xlwt which has a formula module which will store as a formula object in your dataframe.
You can then write this to excel with pandas using df.to_excel like so:
import xlwt
... # your other code here
link = '=HYPERLINK(\"{0}/tr_events.php?triggerid={1}&eventid={2}\"; \"{3}\")'.format(args.url, trigger_id,event['eventid'], event['name'])
excel_formatted = xlwt.Formula(link)
Then when this is passed to excel it should appear as the formula of whatever passed. I only tested it with the LEN() function but it worked fine.

How get a excel sheet with its code name property with "python"

I want to get a Excel's sheet with Python. I can do this with the sheet's name but I want get it with its Code Name property. The following is a code using the sheet's name:
from openpyxl import load_workbook
wb_donnees = load_workbook("Données.xlsm", read_only = True)
name_ws_1 = wb_donnees.get_sheet_name()[0]
ws_1 = wb_donnees[name_ws_1]
But I want get the sheet with its Code Name property. Is it possible ?
Charlie Clark's answer works for me in read mode.
I'm not sure whether OP needed this, but when writing a new workbook, you cannot get the codename this way. Instead, you will need to specify it yourself, otherwise the function returns None, and sheets will only be codenamed 'Sheet1' etc at workbook creation.
wb = load_workbook('input.xlsm')
wsx = wb.create_sheet('New Worksheet')
wsx.sheet_properties.codeName = 'wsx'
wb.save('output.xlsm')
The following should will only work if the file is not opened in read-only mode:
from openpyxl import load_workbook
wb = load_workbook("Données.xlsm")
for n in wb.sheetnames:
ws = wb[n]
print(n, ws.sheet_properties.codeName)

python gspread library only writes to worksheet labeled 'sheet1'

My sheet is named 'doc_name', and it has two worksheets, 'sheet1' and 'sheet2'. but, i can only write data to the worksheet labeled 'sheet1'?
is this a limitation or am i doing something wrong?
this works,
wks = gc.open("doc_name").sheet1
but this fails,
wks = gc.open("doc_name").sheet2
giving this error,
AttributeError: 'Spreadsheet' object has no attribute 'sheet2'
i also notice that this fails,
wks = gc.open("doc_name").Sheet1
...where i use a capital 'S'.. and it will only write if i specify lowercase .sheet1
how do i write to a worksheet without having to code... wks = gc.open("doc_name").sheet1?
This is because gspread only implemented sheet1 to let you retrieve the first sheet in your spreadsheet as a shortcut.
From the source code you can see the implementation of sheet1 is using get_worksheet(0)
#property
def sheet1(self):
"""Shortcut property for getting the first worksheet."""
return self.get_worksheet(0)
So if you want to retrieve other sheets, you need to use other methods like:
1.specify index as a integer indicating the position of the sheet to open starting from 0:
wks = gc.open("doc_name").get_worksheet(index)
or
2.specify title of the sheet to open as a string:
wks = gc.open("doc_name").worksheet(title)
That is to say, in you case, to get sheet2 you can probably use
wks = gc.open("doc_name").get_worksheet(1)
client = gspread.authorize(creds)
sheet = client.open('name').worksheet('name/title')

getting sheet names from openpyxl

I have a moderately large xlsx file (around 14 MB) and OpenOffice hangs trying to open it. I was trying to use openpyxl to read the content, following this tutorial. The code snippet is as follows:
from openpyxl import load_workbook
wb = load_workbook(filename = 'large_file.xlsx', use_iterators = True)
ws = wb.get_sheet_by_name(name = 'big_data')
The problem is, I don't know the sheet name, and Sheet1/Sheet2.. etc. didn't work (returned NoneType object). I could not find a documentation telling me How to get the sheet names for an xlsx files using openpyxl. Can anyone help me?
Use the sheetnames property:
sheetnames
Returns the list of the names of worksheets in this workbook.
Names are returned in the worksheets order.
Type: list of strings
print (wb.sheetnames)
You can also get worksheet objects from wb.worksheets:
ws = wb.worksheets[0]
As a complement to the other answers, for a particular worksheet, you can also use cf documentation in the constructor parameters:
ws.title
python 3.x
for get sheet name you must use attribute
g_sheet=wb.sheetnames
return by list
for i in g_sheet:
print(i)
**shoose any name **
ws=wb[g_sheet[0]]
or ws=wb[any name]
suppose name sheet is paster
ws=wb["paster"]
As mentioned the earlier answer
you can get the list of sheet names
by using the ws.sheetnames
But if you know the sheet names you can get that worksheet object by
ws.get_sheet_by_name("YOUR_SHEET_NAME")
Another way of doing this is as mentioned in earlier answer
ws['YOUR_SHEET_NAME']
for worksheet in workbook:
print(worksheet.name)

Categories