Clear contents of range with openpyxl - python

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()

Related

How to get the value from merged cells in xlsx file using python?

I am trying to get the value from cell with row = 11 and column B and C. See screenshot for more clarification.
I tried following code using xlrd package but it does not print anything.
import xlrd
path = "C:/myfilepath/data.xlsx"
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)
sheet.cell_value(10,1)
sheet.cell_value(10,2)
I am not able to output the value from particular merged cells using xlrd package in python.
Above code should print the cell value i.e PCHGFT001KS
I don't know how xlrd works, but I do know how the lovely openpyxl works. You should use openpyxl! it's a robust tool for working with xlsx files. (NOT xls).
import openpyxl
wb = openpyxl.load_workbook(excel)
ws = wb[wb.get_sheet_names()[0]]
print(ws['B11'].value)
Extra:
If you want to unmerge those blocks you can do the following.
for items in ws.merged_cell_ranges:
ws.unmerge_cells(str(items))
wb.save(excel)

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

"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.

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