Openpyxl Cheatsheet
Openpyxl Cheatsheet
# Opening excel documents with # Charts # Getting a cell using row and column
openpyxl
import openpyxl.chart
# Get the sheet's title as a string
wb_chart = openpyxl.Workbook() from openpyxl.utils import get_column_l‐
sheet_chart = wb_chart.active etter, column_index_from_string
my_titles = sheet.title for i in range(1, 11): col_letter = get_column_letter(1)
sheet_chart['A' + str(i)] = i col_max_letter = get_column_letter(she‐
# Get the active sheet et.max_column)
refObj = openpyxl.chart.Reference(sheet‐ index_letter = column_index_from_string‐
_chart, min_col=1, min_row=1, max_col=1, ('A') # Get A's number
anotherSheet = wb.active
max_row=10)
seriesObj = openpyxl.chart.Series(refObj, # Get the rows, columns
# Getting a cell from the sheet
title='First series')
chartObj = openpyxl.chart.BarChart()
# Using the rows return a tuple of tuples.
cell_A1 = sheet['A1'] chartObj.title = 'My Chart'
Inner tuples - row.
chartObj.append(seriesObj)
# Using the columns return a tuple of tuples.
# Get the value from the cell sheet_chart.add_chart(chartObj, 'C5')
Inner tuples - the cell object in a particular
wb_chart.save('sampleChart.xlsx')
column.
cell_A1_value = sheet['A1'].value # 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.insert_rows(7) tuple_row_1 = list(sheet.rows)[0]
# Ex.: to get the tuple that represents
# Moving ranges. The cells will overwrite column B
cell_A1_row = sheet['A1'].row
tuple_column_B = list(sheet.columns)[1]
cell_A1_column = sheet['A1'].column
cell_A1_coordinate = sheet['A1'].coordinate sheet.move_range("D4:F10", rows=-1,
area_cells = sheet['A1':'C3'] # tuple of all the cols=2)
cell objects
wb_merge = openpyxl.Workbook() # Setting the Font Style of Cells # All rows above and columns to the left of
sheet_merge = wb_merge.active from openpyxl.styles import Font this cell will be frozen
sheet_merge.merge_cells('A1:D3') wb_style = openpyxl.Workbook() # To unfreeze all panes, set freez_panes to
sheet_style = wb_style['Sheet'] None or 'A1'
# To set the value of these merged cells italic24Font = Font(size=24, italic=True, wb_freeze = openpyxl.load_workbook('pro‐
name='Calibri') # Create a font. duceSales.xlsx')
sheet_style['A1'].font = italic24Font # Apply sheet_freeze = wb_freeze.active
sheet_merge['A1'] = 'Twelve cells merged
the font to A1. sheet_freeze.freeze_panes = 'A2' # Freeze
together.'
sheet_style['A1'] = 'Hello, world!' the rows above A2.
sheet_merge.merge_cells('C5:D5')
wb_style.save('styles.xlsx') wb_freeze.save('freezeExample.xlsx')
sheet_merge['C5'] = 'Two merged cells.'
wb_merge.save('merged.xlsx')
# Formulas
# Unmerge cells
# Add formulas to cell just like any normal
value.
wb_unmerge = openpyxl.load_workbook('‐
wb_formulas = openpyxl.Workbook()
merged.xlsx')
sheet_formulas = wb_formulas.active
sheet_unmerge = wb_unmerge.active
sheet_formulas['A1'] = 200
sheet_unmerge.unmerge_cells('A1:D3')
sheet_formulas['A2'] = 300
sheet_unmerge.unmerge_cells('C5:D5')
sheet_formulas['A3'] = '=SUM(A1:A2)' # Set
wb_unmerge.save('unmerged.xlsx')
the formula
wb_formulas.save('writeFormula.xlsx')
# Creating and Removing Sheets