openpyxl模块,统计excel表格第D列的人口数

>>> 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获取值
发布了135 篇原创文章 · 获赞 23 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44478378/article/details/104212213