How to get the CodeName of a sheet - python

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.

Related

How to parse only specific sheets in a workbook using openpyxl - or how to ignore empty sheets?

Well, this is actually a workaround for my main problem which is to "ignore the empty sheets in my workbook". I have found a way to print only those sheet names that are not empty. So, now I want to pass these names to my workbook and access only those sheets instead of every single sheet in wb. (I need to use openpyxl for this.)
I'm trying the below but it doesn't work:
wb = openpyxl.load_workbook("source_file.xlsx", data_only=TRUE)
for ws in wb.get_sheet_by_name(['Sheet1', 'Sheet2', 'Sheet4', 'Sheet5']):
for row in ws:
<do the necessary parsing operations here>
But this throws the below error:
"Worksheet ['Sheet1', 'Sheet2', 'Sheet4', 'Sheet5'] does not exist."
And if I pass the names separately, then it says:
TypeError: get_sheet_by_name() takes 2 positional arguments but 5 were given
Is there a way that I can tell it to access only specific sheets instead of every sheet in wb? Or better, is it possible to ignore all the empty sheets while parsing a .xlsx workbook?
You can store the sheet names in a list, and then iterate over that list to open each sheet:
import openpyxl
wb = openpyxl.load_workbook("source_file.xlsx", data_only=True)
sheets = ['Sheet1', 'Sheet2', 'Sheet4', 'Sheet5']
for sheet in sheets:
for row in wb[sheet]:
# <do the necessary parsing operations here>
Note that you can simply access a sheet from the workbook wb with wb[sheetname]. get_sheet_by_name() is deprecated. See the official documentation.

Obtain name of worksheet using openpyxl

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

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)

Write variable sheets from python to excel

I want to write 250 sheets in Excel with the sheet number as variable. Since then I can be specific which output I want on wich sheet. I tried
from xlwt import Workbook
wb = Workbook()
for i in range(250):
sheeti = wb.add_sheet('Sheet i')
Which gives me logically the error that the worksheet name is duplicated. This is ofcourse because every sheet is now called 'Sheet i'.
Moreover, if I want to write the sheet with the following code
sheeti.write
It gives me that sheeti is not defined.
I can not figure out the solution to this problem. Any help is appreciated.
You're literally naming each sheet 'sheet i'. What you want is 'sheet ' + str(i), or something equivalent.

"Worksheet range names does not exist" KeyError in openpyxl

Let me preface this by saying I have tried looking for, and cannot seem to find a similar situation so please don't be too upset if this seems familiar to you. I am using Python 2.7 and openpyxl version 2.2.5 (I need to use 2.7, and used an older module for other reasons.)
I am new to Python and read/write code in general, so I'm testing this on the command line before I implement it:
I created a file, foo.xlsx in the Python27 file directory with some values that I manually entered via Excel.
I then used this simple code on the Python command line to test my code
from openpyxl import load_workbook
wb = load_workbook('foo.xlsx')
sheet_ranges = wb['range names']
It then resulted in the following error:
File "C:\Python27\lib\openpyxl\workbook.workbook.py", line 233 in getitem
raise KeyError("Worksheet {0} does not exist.".format(key))
KeyError: 'Worksheet sheet range names does not exist'
So I thought it had something to do with not importing the entire openpyxl module. I proceeded to do that and run the whole process but it resulted in the same error.
Can someone please let me know what I am doing wrong/how to solve this?
Additional information:
I had successfully written to an empty file before, and then read the values. This gave me the right values for everything EXCEPT what I had written in manually via Excel- the cells that had manual input returned None or Nonetype. The issue seems to be with cells with manual input.
I did hit save on the file before accessing it don't worry
This was in the same directory so I know that it wasn't a matter of location.
The following command does not make sense:
sheet_ranges = wb['range names']
Normally you open a workbook and then access one of the worksheets, the following gives you some examples on how this can be done:
import openpyxl
wb = openpyxl.Workbook()
wb = openpyxl.load_workbook(filename = 'input.xlsx')
# To display all of the available worksheet names
sheets = wb.sheetnames
print sheets
# To work with the first sheet (by name)
ws = wb[sheets[0]]
print ws['A1'].value
# To work with the active sheet
ws = wb.active
print ws['A1'].value
# To work with the active sheet (alternative method)
ws = wb.get_active_sheet()
print ws['A1'].value
If you want to display any named range in the workbook, you can do the following:
print wb.get_named_ranges()
I'm not exactly sure what it is you need to do, but to read Excel spreadsheets into python, I usually use xlrd (which to me was easier to get use to). See example:
import xlrd
workbook = xlrd.open_workbook(in_fname)
worksheet = workbook.sheet_by_index(0)
To write to Excel spreadsheets, I use xlsxwriter:
import xlsxwriter
workbook = xlsxwriter.Workbook(out_fname)
worksheet = workbook.add_worksheet('spreadsheet_name')
Hope this helps.

Categories