使用openpyxl寫execl確實很方便。我先介紹用到的相關模塊與函數
Workbook:工作簿模塊,在內存創建一個工作簿。
ExcelWriter:使用它向exel中寫數據。
get_column_letter:給一個數字得到一個列名,如A,B,C
模塊安裝:
pip install openpyxl -i https://pypi.tuna.tsinghua.edu.cn/simple
數據寫入到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頁')
