How to iterate over worksheets in workbook, openpyxl - python

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!

Related

How to copy a sheet in one workbook and paste just the values in a new workbook?

I have an excel workbook that has quite a few formulas, and when I try to upload the workbook into a database, the cells with iferror formulas come in as blanks even though it should be a string or number. I am new to python but I want to create a python file that will read in the sheet, and paste only the values into a new workbook.
I tried:
import openpyxl as xl
wb1 = xl.load_workbook('file1.xlsx')
ws1 = wb1["Sheet 1"]
wb2 = xl.load_workbook('file2.xlsx')
ws2 = wb2.create_sheet(ws1.title)
for row in ws1:
for cell in row:
ws2[cell.coordinate].value = cell.value
wb2.save('path')
The code works to copy the data into a new workbook, but it is pasting the formulas. I just want the values.
As per my earlier comment:
This comes from the OpenPyxl docs:
Where it's stated on the openpyxl.reader.excel.load_workbook submodule, looking at the data_only parameter:
data_only (bool) – controls whether cells with formulae have either the formula (default) or the value stored the last time Excel read the sheet
Default is Formulas whereas you want the values. So setting it to true:
wb = xl.load_workbook('file1.xlsx', data_only=True)
Should help :)

Clear contents of range with openpyxl

I'm trying to use a python script to clear data from specific ranges of an Excel spreadsheet. The script runs fine (I've tested printing worksheets to make sure the program is finding them), but the file is unchanged after I run the script and open the file.
from openpyxl import load_workbook
wb = load_workbook(filename = '10.28.17.xlsx')
ws = wb['Players']
for row in ws['A2:E49']:
for cell in row:
cell.value = 0
I would appreciate any assistance or tips you can offer!
You have to save your workbook after your job:
wb.save("file_name.xlsx")
wb.close()

Creating workbook and worksheet using openpyxl

I am trying to load an existing Excel file and create a new sheet inside that workbook, but my code is not working using openpyxl.
rb = load_workbook("C:\Raw_Dump.xlsx")
rb.create_sheet("Sheet2")
sheet1 = rb.worksheets[0]
Any help would be appreciated.
You have to save the workbook to the same filename:
rb.save(r"C:\Raw_Dump.xlsx")
full working example:
import openpyxl
ws_name = r"Raw_Dump.xlsx"
rb = openpyxl.load_workbook(ws_name)
rb.create_sheet("Sheet2")
rb.save(ws_name)
I spent a long time searching this and found the best way is to do sheet removal. The code below worked for me:
for sheet in wb.sheetnames:
if sheet not in "MY_SHEET_I_WANNA_KEEP":
rm_sheet = wb[sheet];
wb.remove_sheet(rm_sheet)
wb.save("JustOneSheet.xlsx")

openpyxl: remove_sheet causes IndexError: list index out of range error on saving sheet

I am trying to use openpyxl to:
Open an Excel (2016) workbook which contains 3 worksheets (Sheet1,Sheet2,Sheet3)
Remove a worksheet (Sheet2)
Save the workbook to a different workbook minus Sheet2
from openpyxl import load_workbook
wb = load_workbook("c:/Users/me/book1.xlsx")
ws = wb.get_sheet_by_name('Sheet2')
wb.remove_sheet(ws)
wb.save("c:/Users/me/book2.xlsx")
The wb.save will generate an IndexError: list index out of range error and produce a corrupted book2.xlsx file which Excel cannot open.
I run into similar problem, only with xlwt library. Regardless, the cause is the same, You remove the sheet which is set as active sheet. So, to fix this, before saving workbook, set some other sheet as active. In openpyxl, it would be something like this:
from openpyxl import load_workbook
wb = load_workbook("c:/Users/me/book1.xlsx")
ws = wb.get_sheet_by_name('Sheet2')
wb.remove_sheet(ws)
wb._active_sheet_index = 0
wb.save("c:/Users/me/book2.xlsx")
I must mention that this is not very good programming practice, but there is no method to set active sheet, only to get one.
EDIT: Just found out that this repo was moved to bitbucket, and found that it has method for setting active sheet. Just use:
wb.active = 0

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.

Categories