氣輕Python114 openpyxl合并單元
?
保存數(shù)據(jù)時(shí),對合并單元的處理。
?
import numpy as np
from openpyxl import Workbook
import datetime
import webbrowser
from openpyxl.styles import *
from openpyxl import load_workbook
?
########################################################@####################
#?? qipy114 : 合并單元????????????????????????????????????????????????????? #
########################################################@####################
def qipy114():
??? fileName = "Data.xlsx"
??? wb = load_workbook(fileName)??????????????????????? # 讀取數(shù)據(jù)
??? ws = wb['Sheet1']
??? rowsMax = ws.max_row
??? colMax = ws.max_column
?
??? fname = 'Data1.xlsx'
??? wb = Workbook()
?
??? ws = wb.active
?
??? ws.merge_cells('A1:A2')
??? ws.cell(1,1, '日期')
??? ws.cell(1,1).font = Font(size=16)
?
??? timeLabel = ['早', '中', '晚']
??? tmp = ['B1:C1','D1:E1','F1:G1']
??? header = dict(zip(timeLabel,tmp))
??? for i, s in enumerate(header.keys()):
??????? ws.merge_cells(header[s])
?? ?????ws.cell(1,(i + 1)*2, s)
??????? ws.cell(1, (i + 1)*2).font = Font(size=16)
?
??? timeLabel = ['飯前', '飯后']*3 + [ '體重']
??? for i, s in enumerate(timeLabel):
??????? ws.cell(2,(i + 2), s)
??????? ws.cell(2, i + 2).font = Font(size=16)
?
??? wb.save(fname)
??? webbrowser.open(fname)
?
??? return
?
if __name__ == "__main__":
??? qipy114()
?
執(zhí)行結(jié)果
