>>> import openpyxl
>>> wb = openpyxl.load_workbook('censuspopdata.xlsx')
>>> wb.sheetnames
['Population by Census Tract']
>>> wb.sheetnames # 获取工作簿名称
['Population by Census Tract']
>>> sheet = wb['Population by Census Tract']
>>> sheet.max_row # 获取最大行数
72865
>>> county = 0
>>> for row in range(2, sheet.max_row +1):
county += sheet['D' + str(row)].value
>>> county
308745538
>>>
- excel表格中:人口数据在第D列
- excel第一行为名称行,故循环从第二行开始
sheet['D' + str(row)].value
- sheet[’’]获取单元格
- .value获取值