python操作excel合并单元格

荒凉一梦 提交于 2019-12-17 18:58:03
import xlwt
import xlwings as xw

def set_style(name, height, bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.bold = bold
    font.colour_index = 4
    font.height = height
    font.name = name
    style.font = font
    return style

def write_excel():
    f = xlwt.Workbook()

    sheet1 = f.add_sheet(u"total", cell_overwrite_ok=True)
    row0 = [u"区域", u"项目", u"2019年5月当月实际", u"2019年5月当月预算", u"2019年5月当月预算完成率", u"2019年5月累计实际", u"2019年5月累计预算"
            , u"2019年5月累计预算完成率", u"项目小计", u"合计"]
    columns0 = [u"北京", u"天津", u"海南", u"杭州", u"武汉", u"重庆",]
    status = [u"营业收入", u"营业成本", u"费用合计", u"营业外收支", u"税金合计", u"利润总额"]
    for i in range(0, len(row0)):
        sheet1.write(0, i, row0[i], set_style("微软雅黑", 220, True))

    i,j = 1,0
    while i < 6*len(columns0) and j<len(columns0):

        sheet1.write_merge(i,i+5,0,0,columns0[j],set_style("微软雅黑", 220, True))
        sheet1.write_merge(i,i+5,9,9)
        i +=6
        j +=1
    sheet1.write_merge(37,37,0,1,u"合计",set_style("微软雅黑", 220, True))

    i=0
    while i < 6*len(columns0):
        for j in range(0,len(status)):
            sheet1.write(i+j+1,1,status[j])
        i +=6
        f.save("D:/PY/data_1217.xls")


if __name__ == '__main__':
    write_excel()


结果:
在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!