14 December 2020

Manipulate Spreadsheets in Python using openpyxl

Prerequisites

If you don't have pip, install that first. Then install openpyxl using pip: run pip install openpyxl

Creating and Loading .xlsx Workbooks

Using openpyxl, we can create a new Excel Workbook:

from openpyxl import Workbook
workbook_filename = 'sample.xlsx'
wb = Workbook() # Create new

Or alternatively we can open an existing one. To do this we also need to import the load_workbook function:

from openpyxl import Workbook, load_workbook
workbook_filename = 'sample.xlsx'
wb = load_workbook(i_want_to_create_a_new_file)

Creating and Opening Sheets

Once we have opened our workbook, we can create a new sheet or open an existing one.

Create a new sheet

sheet_name = 'Sheet'
ws = wb.create_sheet(sheet_name)

Or open an existing one:

sheet_name = 'Sheet'
ws = wb[sheet_name]

Accessing Cells

With an open sheet, we can set and get cell values using the cell key:

# Set a cell value
ws['A1'] = 42
# Get a cell value
value_from_cell = ws['A1'].value
print("value_from_cell: ", value_from_cell)

Alternatively, we can access a cell from its column and row position, starting at column=1, row=1 for the cell A1.

x = ws.cell(column=1, row=1) 
print("x: ", x.value)

Output: x: 42

Note that when using this method to set a value, you cannot write

ws.cell(column=1, row=1) = value #WILL NOT WORK!

You have to do it as follows:

ws.cell(column=1, row=1, value=300)

Note that doing it this way allows for things like the following:

for i in range(1, 10):
    ws.cell(column=i, row=2, value=i)

That is:

For each number i, in the range 1 to 10:
    set the cell at position (i, 2) to i

Note: up to 10, not up to and including 10.

Save the file

wb.save(workbook_filename)

Full example

Tags: Python