Workbooks, Sheets, Cells
As a quick review, here’s a rundown of all the functions, methods, and data types involved in reading a cell out of a spreadsheet file:
-
Import the
openpyxl
module. -
Call the
openpyxl.load_workbook()
function. -
Get a
Workbook
object. -
Call the
get_active_sheet()
orget_sheet_by_name()
workbook method. -
Get a
Worksheet
object. -
Use indexing or the
cell()
sheet method withrow
andcolumn
keyword arguments. -
Get a
Cell
object. -
Read the
Cell
object’svalue
attribute.
setdefault()
for row in range(2, sheet.get_highest_row() + 1):
# Each row in the spreadsheet has data for one census tract.
state = sheet['B' + str(row)].value
county = sheet['C' + str(row)].value
pop = sheet['D' + str(row)].value
# Make sure the key for this state exists.
#Since setdefault() will do nothing if the key already exists,
#you can call it on every iteration of the for loop without a problem.
countyData.setdefault(state, {})
# Make sure the key for this county in this state exists.
countyData[state].setdefault(county, {'tracts': 0, 'pop': 0})
# Each row represents one census tract, so increment by one.
countyData[state][county]['tracts'] += 1
# Increase the county pop by the pop in this census tract.
countyData[state][county]['pop'] += int(pop)
Since setdefault()
will do nothing if the key already exists, you can call it on every iteration of the
for
loop without a problem.
Write the Results to a File
# Open a new text file and write the contents of countyData to it.
print('Writing results...')
resultFile = open('census2010.py', 'w')
resultFile.write('allData = ' + pprint.pformat(countyData))
resultFile.close()
print('Done.')
The readCensusExcel.py program was throwaway code: Once you have its results saved tocensus2010.py,
you won’t need to run the program again. Whenever you need the county data, you can just runimport census2010
.
Project: Updating a Spreadsheet
Step 1: Set Up a Data Structure with the Update Information
A more flexible solution is to store the corrected price information in a dictionary and write your code to use this data structure.
PRICE_UPDATES = {'Garlic': 3.07,
'Celery': 1.19,
'Lemon': 1.27}
If you need to update the spreadsheet again, you’ll need to update only the
PRICE_UPDATES
dictionary, not any other code.