Overview excel structure


Install working environment

Install python & openpyxl with anaconda

Both Python and openpyxl could be installed by Anaconda. Go to download page and download Anaconda for Python 3.6. After download, just double click to install with default option.


After install, open command prompt and typing in python . The anaconda and python version should show up as below


From python shell, import openpyxl to make sure that it is installed successfully.


Install Sublime as python IDE

In this course I use Sublimetext as Python IDE to writing code (you can choose any other text edition you want). I choose Sublimtext due to :

Part 1 : Working with existing xlsx file

Loading workbook from local file

First thing we need to do is import module openpyxl with

import openpyxl

This module provide function allow us to to create a workbook or load a workbook from a file. In the first part of this course we will load a existing workbook from a 500.xlsx file.

# loading workbook from file
wb = openpyxl.load_workbook('500.xlsx')

Access worksheet from workbook

After load workbook, we could access current active worksheet with code. Only has one active worksheet at a time, and you could access data only from active worksheet. Default active worksheet is the first sheet.

# get current active worksheet
current_active_worksheet = wb.get_active_sheet()

To get name of all worksheets

# get all sheet name
all_sheet_names = wb.get_sheet_names()

Now, you can set active sheet by name

current_active_worksheet = wb.get_sheet_by_name('uk-500')

Following loop could help to access all sheet

# for loop to access all sheet inside workbook
for ws in wb:

Access one cell from worksheet

With MS Excel:

One cell could be accessed simply specified by combination column and row. And then actual data could be accessed with value cell property.

# print out data at column A and row 2 of current worksheet
# set value for one cell
ws['A2'] = 'Tan Pham'
# print out new data

Access one row, column from worksheet

If we only specify just column, we could access all cells inside that column

# print all data from column A (the first column)
for cell in ws['A']:

If we only specify just row, we could access all cells inside that row

# print all data from row 1, which are columns name
for cell in ws['1']:

Access all rows and columns from worksheet

Incase we want to scan the whole sheet, openpyl provide property call rows and columns.

# access all data , row by row
for row in ws.rows:
    for cell in row:
# access all data , column by column
for column in ws.columns:
	for cell in column:

Save workbook

After doing modify data, to save workbook with name.

# save workbook with name

Part 2 : Working with new xlsx file

Create a new workbook

To create a new empty workbook. A workbook is always created with at least one worksheet.

from openpyxl import Workbook
# create a new workbook
wb = Workbook()
# get the default worksheet
ws = wb.active
# set worksheet title
ws.title = "first sheet"

Add cell data

Cell data is set by combination of column and row.

# set value for first row as column name
ws['A1']='first column'
ws['B1']='second column'

# set value for second row

Save workbook


Bonus : Document Reference Link