使用openpyxl写execl确实很方便。我先介绍用到的相关模块与函数
Workbook:工作簿模块,在内存创建一个工作簿。
ExcelWriter:使用它向exel中写数据。
get_column_letter:给一个数字得到一个列名,如A,B,C
数据写入到EXCEL表格
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
data = [
['序号', '组号', '申报单位', '姓名', '准考号', '考试分数(卷面分)']
, ['1', '1', '新民采油厂', '刘铁', '2012051224', '67.834']
, ['2', '1', '吉林油田总医院', '吕册', '2012120214', '66.776']
, ['3', '1', '吉林油田总医院', '王彦苏', '2012120718', '66.683']
, ['4', '1', '农业开发公司', '刘化峰', '2012060120', '66.664']
, ['5', '1', '油区教育处', '沙丽珊', '2012020817', '66.657']
, ['6', '1', '油区教育处', '王志会', '2012020914', '66.545']
, ['7', '1', '扶余采油厂', '姜波', '2012041210', '66.355']
, ['8', '2', '红岗采油厂', '王昕', '2012021226', '66.3']
, ['9', '2', '通信公司', '彭莉', '2012121014', '66.17']
, ['10', '2', '前郭矿区', '米云龙', '2012070121', '65.313']
, ['11', '2', '油区教育处', '秦勤', '2012022620', '65.061']
, ['12', '2', '储运销售公司', '许华', '2012130406', '64.95']
, ['13', '2', '江南物业', '兰庆伟', '2012030620', '64.869']
, ['14', '2', '滨江物业', '佟冬蕾', '2012110417', '64.652']
, ['15', '2', '农业开发公司', '曹宽', '2012061025', '64.564']
, ['16', '2', '农业开发公司', '孙成伟', '2012060318', '64.462']
, ['17', '3', '红岗采油厂', '张大勇', '2012020112', '64.384']
, ['18', '3', '储运销售公司', '刘贤宇', '2012130721', '64.378']
, ['19', '3', '建设公司', '李月', '2012010316', '64.029']
, ['20', '3', '客运公司', '姚佳媛', '2012130613', '63.914']
, ['21', '3', '物资供应处', '张丽', '2012110925', '63.883']
, ['22', '3', '江北物业', '益长虹', '2012100222', '63.723']
, ['23', '3', '建设公司', '张雁冰', '2012013327', '63.630']
, ['24', '3', '公用事业管理公司', '王涵', '2012022214', '63.522']
]
# 在内存创建一个工作簿obj
wb = Workbook()
ws = wb.active
ws.title = u'招录人员名单'
# 向第一个sheet页写数据吧
i = 1
r = 1
for line in data:
for col in range(1, len(line) + 1):
ColNum = r
ws.cell(row=r, column=col).value = line[col - 1]
i += 1
r += 1
# 工作簿保存到磁盘
wb.save('test.xlsx')
效果图:
数据第二列是组号,数字相同的组号放在一个单元格中,怎么办呢?
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
data = [
['序号', '组号', '申报单位', '姓名', '准考号', '考试分数(卷面分)']
, ['1', '1', '新民采油厂', '刘铁', '2012051224', '67.834']
, ['2', '1', '吉林油田总医院', '吕册', '2012120214', '66.776']
, ['3', '1', '吉林油田总医院', '王彦苏', '2012120718', '66.683']
, ['4', '1', '农业开发公司', '刘化峰', '2012060120', '66.664']
, ['5', '1', '油区教育处', '沙丽珊', '2012020817', '66.657']
, ['6', '1', '油区教育处', '王志会', '2012020914', '66.545']
, ['7', '1', '扶余采油厂', '姜波', '2012041210', '66.355']
, ['8', '2', '红岗采油厂', '王昕', '2012021226', '66.3']
, ['9', '2', '通信公司', '彭莉', '2012121014', '66.17']
, ['10', '2', '前郭矿区', '米云龙', '2012070121', '65.313']
, ['11', '2', '油区教育处', '秦勤', '2012022620', '65.061']
, ['12', '2', '储运销售公司', '许华', '2012130406', '64.95']
, ['13', '2', '江南物业', '兰庆伟', '2012030620', '64.869']
, ['14', '2', '滨江物业', '佟冬蕾', '2012110417', '64.652']
, ['15', '2', '农业开发公司', '曹宽', '2012061025', '64.564']
, ['16', '2', '农业开发公司', '孙成伟', '2012060318', '64.462']
, ['17', '3', '红岗采油厂', '张大勇', '2012020112', '64.384']
, ['18', '3', '储运销售公司', '刘贤宇', '2012130721', '64.378']
, ['19', '3', '建设公司', '李月', '2012010316', '64.029']
, ['20', '3', '客运公司', '姚佳媛', '2012130613', '63.914']
, ['21', '3', '物资供应处', '张丽', '2012110925', '63.883']
, ['22', '3', '江北物业', '益长虹', '2012100222', '63.723']
, ['23', '3', '建设公司', '张雁冰', '2012013327', '63.630']
, ['24', '3', '公用事业管理公司', '王涵', '2012022214', '63.522']
]
# 在内存创建一个工作簿obj
wb = Workbook()
ws = wb.active
ws.title = u'招录人员名单'
# 向第一个sheet页写数据吧
i = 1
r = 1
for line in data:
for col in range(1, len(line) + 1):
ColNum = r
ws.cell(row=r, column=col).value = line[col - 1]
i += 1
r += 1
# =========合并单元格区===========
Li = []
tmp1 = []
tmp2 = []
tmp3 = []
for line in data: # 枚举法,总共三组数据,求出每组数据的数量
v = line[1]
if v == '1':
tmp1.append(v)
if v == '2':
tmp2.append(v)
if v == '3':
tmp3.append(v)
Li.append(tmp1)
Li.append(tmp2)
Li.append(tmp3)
# 定义合并单元格的开始位置
start_row = 2
end_row = 2
start_column = 2
end_column = 2
for each in Li:
end_row = start_row + len(each) - 1
ws.merge_cells(start_row=start_row, end_row=end_row, start_column=start_column, end_column=end_column)
start_row = end_row + 1
# =========合并单元格区===========
# 工作簿保存到磁盘
wb.save('test.xlsx')
效果图:
我想再创建一个sheet页
# 再创建一个sheet页
ws = wb.create_sheet(title=u'第二个Sheet页')