Updating excel files from closed worksheets
26-Mar-2020 03:10
If you want, you can load a workbook, get a Say you have a spreadsheet of data from the 2010 US Census and you have the boring task of going through its thousands of rows to count both the total population and the number of census tracts for each county.(A census tract is simply a geographic area defined for the purposes of the census.) Each row represents a single census tract.❶ import openpyxl, pprint print('Opening workbook...') ❷ wb = openpyxl.load_workbook('censuspopdata.xlsx') ❸ sheet = wb.get_sheet_by_name('Population by Census Tract') county Data = # TODO: Fill in county Data with each county's population and tracts.print('Reading rows...') ❹ for row in range(2, sheet.max_row 1): # Each row in the spreadsheet has data for one census tract.
Even if it takes just a few seconds to calculate a county’s population by hand, this would take hours to do for the whole spreadsheet.state = sheet['B' str(row)].value county = sheet['C' str(row)].value pop = sheet['D' str(row)].value # TODO: Open a new text file and write the contents of county Data to it., which will contain the populations and number of tracts you calculate for each county.