Write variable sheets from python to excel - python

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.

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.

does this library assume the Google Spreadsheet will have one sheet only?

I am trying to use this library to pull data from a Googlespreadsheet with two sheets in it, I can get data only from the first sheet but not the second sheet. sheet = client.open("sheetname").sheet1, if I change sheet1 to sheet2 I get the following error sheet = client.open("filename").sheet2 AttributeError: 'Spreadsheet' object has no attribute 'sheet2' how do I fix this? any help is appreciated!
.sheet1 is used as a shortcut.
In order to get the second sheet try that:
sheet = client.open("filename").get_worksheet(1)
1 means second sheet (starting from 0).
References:
Official documentation
In this case, you can use get_worksheet, worksheet and worksheets.
Sample script:
sh = client.open("###Spreadsheet name###") # or client.open_by_key(spreadsheetId)
worksheet = sh.get_worksheet(1) # Use the index of the sheet. 0 is the 1st sheet.
worksheet = sh.worksheet('Sheet2') # Use the sheet name of the sheet.
worksheet = sh.worksheets()[1] # In this case, all sheets are included in the array.
Note:
In the current stage, it seems that sh.sheet1 is only the 1st sheet.
Reference:
Selecting a Worksheet

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.

Python: Write a dataframe to an already existing excel which contains a sheet with images

I have been working on this for too long now. I have an Excel with one sheet (sheetname = 'abc') with images in it and I want to have a Python script that writes a dataframe on a second separate sheet (sheetname = 'def') in the same excel file. Can anybody provide me with some example code, because everytime I try to write the dataframe, the first sheet with the images gets emptied.
This is what I tried:
book = load_workbook('filename_of_file_with_pictures_in_it.xlsx')
writer = pd.ExcelWriter('filename_of_file_with_pictures_in_it.xlsx', engine = 'openpyxl')
writer.book = book
x1 = np.random.randn(100, 2)
df = pd.DataFrame(x1)
df.to_excel(writer, sheet_name = 'def')
writer.save()
book.close()
It saves the random numbers in the sheet with the name 'def', but the first sheet 'abc' now becomes empty.
What goes wrong here? Hopefully somebody can help me with this.
Interesting question! With openpyxl you can easily add values, keep the formulas but cannot retain the graphs. Also with the latest version (2.5.4), graphs do not stay. So, I decided to address the issue with
xlwings :
import xlwings as xw
wb = xw.Book(r"filename_of_file_with_pictures_in_it.xlsx")
sht=wb.sheets.add('SheetMod')
sht.range('A1').value = np.random.randn(100, 2)
wb.save(r"path_new_file.xlsx")
With this snippet I managed to insert the random set of values and saved a new copy of the modified xlsx.As you insert the command, the excel file will automatically open showing you the new sheet- without changing the existing ones (graphs and formulas included). Make sure you install all the interdependencies to get xlwings to run in your system. Hope this helps!
You'll need to use an Excel 'reader' like Openpyxl or similar in combnination with Pandas for this, pandas' to_excel function is write only so it will not care what is inside the file when you open it.

How to iterate over worksheets in workbook, openpyxl

I've been using the openpyxl module to do some processing on some .xlsx files. I've been trying to figure out how to iterate over sheets in a workbook. I'm not sure if I can get it figured out. I've tried the 2 codes below which both return empty results. My .xlsx file has about 20 sheets, so something should return.
The one thing I couldn't find on the internet, is how to set a workbook to an actual workbook. Usually I am writing to a workbook, so I just initialize it by setting a variable to en empty workbook workbook = Workbook() but in this case, I am unsure if I can open a workbook by doing workbook = Workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
If anyone can identify what it is I am doing wrong, I would appreciate it.
Here is my code:
workbook = Workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
for sheet in workbook.worksheets:
print sheet
# or
for sheet in workbook.worksheets:
print sheet.title
Open the workbook via load_workbook() and iterate over worksheets:
from openpyxl import load_workbook
wb = load_workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
for sheet in wb.worksheets:
print(sheet)
Here's one if you need active worksheets for your code
for sheet in wb:
ws = wb[sheet]
print('Now in sheet: ' + ws.title)
To print titles of all sheets in a workbook:
from openpyxl import load_workbook
wb = load_workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
print(wb.sheetnames)
I struggled a bit with the whole "workbook.active" and didn't know how to get around it so I tried a little bit of everything and here is what worked well for me!
for sheet in workbook.sheetnames[2:len(workbook.sheetnames)]:
ws = workbook[value]
for val in ws.iter_rows(min_row=11, max_row=21, min_col=2, max_col=10, values_only=True):
print(str(sheet) + " " + str(val))
This will print the sheet name starting with the third sheet, since that's what I needed, as well as all the cell values referenced. The only other thing is this prints a list and if you want to iterate through each value and pull out "0s" or "None" values, then you'll need another loop. Hope this helps whoever else is looking!

Categories