1、涉及内容:openpyxl.load_workbook、workbook.sheetnames、workbook['表名']、sheet['A1'].row 、sheet['A1'].column
example.xlsx:
'''
openpyxl模块
1、用 openpyxl.load_workbook()方法返回workbook对象
2、workbook对象的sheetnames属性、get_sheet_by_name:通过表格名称获取Worksheet对象(新版通过Worksheet['表名']获取)
'''
# !/usr/bin/python
import openpyxl, os
w = openpyxl.load_workbook(
'example.xlsx') # openpyxl.load_workbook()函数接受文件名,返回一个 workbook 数据类型的值。这 个 workbook 对象代表这个 Excel 文件
# print(os.getcwd()) 这句可以用来弄清楚当前目录是什么
print(w.sheetnames) # sheetnames属性:获取工作簿中的表(列表)
s1 = w['工作表1']
print(s1['A1'].value) # openpyxl 模块将自动解释列 A 中的日期,将它们返回为datetime值,而不是字符串
print(s1['B1'].value)
print(s1['C1'].value)
print(s1['B1'].row, s1['B1'].column, s1['B1'].coordinate) # 打印表s1中B1单元格的行值、列值、坐标值
print('---------------------------')
# 除了用字母来指定列,还可用表的cell()方法,传入整数作为row 和 column 关键字参数
print(s1.cell(row=1, column=2).value)
for i in range(1, 6):
print(s1.cell(row=i, column=2).value)
print('表格的最大行=' + str(s1.max_row), '表格的最大列=' + str(s1.max_column))
print('***************************')
for row in s1.iter_rows(): # iter_rows:按行获取所有单元格(所有cell),内置属性有(min_row,max_row,min_col,max_col)
print(row) # 一个cell对象代表一个单元格
# 打印表s1中的所有数据
for row in s1.rows:
print(*[cell.value for cell in row])
打印结果:
'''
['工作表1', '是表2', '表3']
2015/5/4
Apples
73
1 B B1
---------------------------
Apples
Apples
Cherries
Pears
Oranges
Apples
表格的最大行=7 表格的最大列=3
***************************
(<Cell '工作表1'.A1>, <Cell '工作表1'.B1>, <Cell '工作表1'.C1>)
(<Cell '工作表1'.A2>, <Cell '工作表1'.B2>, <Cell '工作表1'.C2>)
(<Cell '工作表1'.A3>, <Cell '工作表1'.B3>, <Cell '工作表1'.C3>)
(<Cell '工作表1'.A4>, <Cell '工作表1'.B4>, <Cell '工作表1'.C4>)
(<Cell '工作表1'.A5>, <Cell '工作表1'.B5>, <Cell '工作表1'.C5>)
(<Cell '工作表1'.A6>, <Cell '工作表1'.B6>, <Cell '工作表1'.C6>)
(<Cell '工作表1'.A7>, <Cell '工作表1'.B7>, <Cell '工作表1'.C7>)
2015/5/4 Apples 73
2015/5/5 Cherries 85
2015/5/6 Pears 14
2015/5/7 Oranges 52
2015/5/8 Apples 152
2015/5/9 Bananas 23
2015/5/10 Strawberries 98
'''
2、将excel里的数据存到.py文件中,方便其他py程序调用(直接import)
涉及:字典的setdefault方法、pprint.pformat
censuspopdata.xlsx:可以从 http://nostarch.com/automatestuff/下载它
'''
计算所有普查区和人口数据,将它保存到一个数据结构中。
利用 pprint 模块,将该数据结构写入一个扩展名为.py 的文本文件。
'''
import openpyxl, pprint
print('opening workbook...')
w = openpyxl.load_workbook('censuspopdata.xlsx') # 获得workbook对象
s1 = w['Population by Census Tract']
countyData = {} # 保存在 countyData 中的数据结构将是一个字典
print('reading rows...')
for row in range(2, s1.max_row + 1):
State = s1['B' + str(row)].value # 将s1的B列的值(value)赋给State变量
county = s1['C' + str(row)].value # 将s1的C列的值依次赋给county
pop = s1['D' + str(row)].value
countyData.setdefault(State, {}) # 用字典的setdefault方法填充数据结构《python编程快速上手》P110
countyData[State].setdefault(county, {'tracts': 0, 'pop': 0})
countyData[State][county]['pop'] += int(pop)
# 用open、file.write、pprint.pformat将变量字典的值作为一个字符串,写入文件census2010.py中
print('writing result')
resultfile = open('census2010.py', 'w')
resultfile.write('alldata=' + pprint.pformat(countyData)) # pprint.pformat()函数产生一个字符串,它本身就是格式化好的、有效的 Python 代码
resultfile.close()
print('Done')
'''
打印结果:
opening workbook...
reading rows...
writing result
Done
'''
在当前python package目录下生成census2010.py 文件
文件内容如下(只截取了部分):