0% found this document useful (0 votes)
454 views2 pages

Openpyxl Cheatsheet

Uploaded by

RATATATA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
454 views2 pages

Openpyxl Cheatsheet

Uploaded by

RATATATA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

openPyXL_cheatsheet Cheat Sheet

by Dima via cheatography.com/128706/cs/25257/

# Opening excel documents with # Charts # Getting a cell using row and column
openpyxl

# 1. Create a Reference object from a cell_B1 = sheet.c​el​l(r​ow=1, column=2) # if


import openpyxl rectan​gular selection of cells. add argument 'value=' it'll change the value
wb = openpy​xl.l​oa​d_w​ork​boo​k('​exa​mpl​e.x​‐ # 2. Create a Series object by passing in of cell
lsx') the Reference object.
# 3. Create a Chart object. # Get the highest row number
# Getting sheets from the workbook # 4. Append the Series object to the Chart
object.
sheet_​max_row = sheet.m​ax_row
# 5. Add the Chart object to the Worksheet
my_she​etnames = wb.she​etnames # return
object, optionally specifying which cell
list object # Get the highest column number
should be
# the top-left corner of the chart.
# Get a sheet from the workbook
# Ex.: BarChart() sheet_​max​_column = sheet.m​ax​_column

sheet = wb[my_​she​etn​ame​s[0]] # sheet3 for # Charts # Converting between column letters


example and numbers

import openpy​xl.c​hart
# Get the sheet's title as a string
wb_chart = openpy​xl.W​or​kbook() from openpy​xl.u​tils import get_co​lum​n_l​‐
sheet_​chart = wb_cha​rt.a​ctive etter, column​_in​dex​_fr​om_​string
my_titles = sheet.t​itle for i in range(1, 11): col_letter = get_co​lum​n_l​ett​er(1)
sheet_​cha​rt['A' + str(i)] = i col_ma​x_l​etter = get_co​lum​n_l​ett​er(​she​‐
# Get the active sheet et.m​ax​_co​lumn)
refObj = openpy​xl.c​ha​rt.R​ef​ere​nce​(sh​eet​‐ index_​letter = column​_in​dex​_fr​om_​str​ing​‐
_chart, min_col=1, min_row=1, max_col=1, ('A') # Get A's number
anothe​rSheet = wb.active
max_ro​w=10)
seriesObj = openpy​xl.c​ha​rt.S​er​ies​(re​fObj, # Get the rows, columns
# Getting a cell from the sheet
title=​'First series')
chartObj = openpy​xl.c​ha​rt.B​ar​Chart()
# Using the rows return a tuple of tuples.
cell_A1 = sheet[​'A1'] chartO​bj.t​itle = 'My Chart'
Inner tuples - row.
chartO​bj.a​pp​end​(se​rie​sObj)
# Using the columns return a tuple of tuples.
# Get the value from the cell sheet_​cha​rt.a​dd​_ch​art​(ch​artObj, 'C5')
Inner tuples - the cell object in a particular
wb_cha​rt.s​av​e('​sam​ple​Cha​rt.x​lsx')
column.
cell_A​1_value = sheet[​'A1​'].v​alue # Convert to list with the list() function. Use
# Insert row
index in the larger tuple.
# Get the row, column, coordinate from # Ex.: to get the tuple that represents row 1
the cell sheet.i​ns​ert​_ro​ws(7) tuple_​row_1 = list(s​hee​t.r​ows)[0]
# Ex.: to get the tuple that represents
# Moving ranges. The cells will overwrite column B
cell_A​1_row = sheet[​'A1​'].row
tuple_​col​umn_B = list(s​hee​t.c​olu​mns)[1]
cell_A​1_c​olumn = sheet[​'A1​'].c​olumn
cell_A​1_c​oor​dinate = sheet[​'A1​'].c​oo​rdinate sheet.m​ov​e_r​ang​e("D​4:F​10", rows=-1,
area_cells = sheet[​'A1​':'C3'] # tuple of all the cols=2)
cell objects

By Dima Published 17th November, 2020. Sponsored by CrosswordCheats.com


cheatography.com/dima/ Last updated 17th November, 2020. Learn to solve cryptic crosswords!
Page 1 of 2. http://crosswordcheats.com
openPyXL_cheatsheet Cheat Sheet
by Dima via cheatography.com/128706/cs/25257/

# Merging and Unmerging Cells # Module openpy​xl.s​tyles # Freezing Panes

wb_merge = openpy​xl.W​or​kbook() # Setting the Font Style of Cells # All rows above and columns to the left of
sheet_​merge = wb_mer​ge.a​ctive from openpy​xl.s​tyles import Font this cell will be frozen
sheet_​mer​ge.m​er​ge_​cel​ls(​'A1​:D3') wb_style = openpy​xl.W​or​kbook() # To unfreeze all panes, set freez_​panes to
sheet_​style = wb_sty​le[​'Sh​eet'] None or 'A1'
# To set the value of these merged cells italic​24Font = Font(s​ize=24, italic​=True, wb_freeze = openpy​xl.l​oa​d_w​ork​boo​k('​pro​‐
name='​Cal​ibri') # Create a font. duc​eSa​les.xlsx')
sheet_​sty​le[​'A1​'].font = italic​24Font # Apply sheet_​freeze = wb_fre​eze.active
sheet_​mer​ge[​'A1'] = 'Twelve cells merged
the font to A1. sheet_​fre​eze.fr​eez​e_panes = 'A2' # Freeze
together.'
sheet_​sty​le[​'A1'] = 'Hello, world!' the rows above A2.
sheet_​mer​ge.m​er​ge_​cel​ls(​'C5​:D5')
wb_sty​le.s​av​e('​sty​les.xlsx') wb_fre​eze.sa​ve(​'fr​eez​eEx​amp​le.x​lsx')
sheet_​mer​ge[​'C5'] = 'Two merged cells.'
wb_mer​ge.s​av​e('​mer​ged.xlsx')
# Formulas

# Unmerge cells
# Add formulas to cell just like any normal
value.
wb_unmerge = openpy​xl.l​oa​d_w​ork​boo​k('​‐
wb_for​mulas = openpy​xl.W​or​kbook()
mer​ged.xlsx')
sheet_​for​mulas = wb_for​mul​as.a​ctive
sheet_​unmerge = wb_unm​erg​e.a​ctive
sheet_​for​mul​as[​'A1'] = 200
sheet_​unm​erg​e.u​nme​rge​_ce​lls​('A​1:D3')
sheet_​for​mul​as[​'A2'] = 300
sheet_​unm​erg​e.u​nme​rge​_ce​lls​('C​5:D5')
sheet_​for​mul​as[​'A3'] = '=SUM(​A1:A2)' # Set
wb_unm​erg​e.s​ave​('u​nme​rge​d.x​lsx')
the formula
wb_for​mul​as.s​av​e('​wri​teF​orm​ula.xlsx')
# Creating and Removing Sheets

# Setting Row Height and Column Width


wb_new.cr​eat​e_s​heet() # Add a new sheet
wb_new.cr​eat​e_s​hee​t(i​ndex=0, title=​'First
wb_dim​ension = openpy​xl.W​or​kbook()
sheet') # Create a new sheet at index 0
sheet_​dim​ension = wb_dim​ens​ion.active
wb_new.cr​eat​e_s​hee​t(i​ndex=2,
sheet_​dim​ens​ion​['A1'] = 'Tall row'
title=​'Middle sheet') # Create a new sheet at
sheet_​dim​ens​ion​['B2'] = 'Wide column'
index 2
sheet_​dim​ens​ion.ro​w_d​ime​nsi​ons​[1].height
del wb_new​['M​iddle sheet'] # Remember to
= 70 # Set the height
call the save() method to save changes
sheet_​dim​ens​ion.co​lum​n_d​ime​nsi​ons​‐
['B​'].w​idth = 20 # Set the width
# Writing Values to Cells
sheet_​dim​ens​ion.co​lum​n_d​ime​nsi​ons​‐
['C​'].h​idden = True # Hide the column 'C'
# Writing values to cells is much like writing wb_dim​ens​ion.sa​ve(​'di​men​sio​ns.x​lsx')
values to keys in a dictio​nary.
sheet_​new​['A1'] = 'Hello, world!'
print(​she​et_​new​['A​1'].value)

By Dima Published 17th November, 2020. Sponsored by CrosswordCheats.com


cheatography.com/dima/ Last updated 17th November, 2020. Learn to solve cryptic crosswords!
Page 2 of 2. http://crosswordcheats.com

You might also like