把Excel里的内容(表格)通过Python-docx转化为word里的文字段落内容

前言:具体的需求可以看我之前的文章《通过VBA和Python把excel的结果数据转化成word点评》,当初有一个遗憾:文字内容其实是放到了txt里而不是word,导致我后续还要自己复制黏贴过去并根据大标题、小标题、正文等内容设置不同的格式。所以找了一些比较好用的轮子,最后决定使用python-docx,代码如下:

import os
import datetime
from win32com.client import Dispatch
from docx import Document
from docx.shared import Inches, RGBColor, Pt, Length
from docx.enum.text import WD_ALIGN_PARAGRAPH

def chinese_rank(jjc_range):
    if jjc_range == 1:
        return "第一名:"
    elif jjc_range == 2:
        return "第二名:"
    elif jjc_range == 3:
        return "第三名:"
    elif jjc_range == 4:
        return "第四名:"
    elif jjc_range == 5:
        return "第五名:"
    elif jjc_range == 6:
        return "第六名:"
    else:
        print("好像有点问题,确认一下代码或者代理商数量!")

def get_last_month():
    today = datetime.date.today()
    month = int(today.strftime("%m"))
    if month == 1: return '12月'
    else: return str(month -1) + '月'

def today_format():
    today = datetime.date.today()
    today_format = today.strftime("%Y"+'/'+"%m"+'/'+"%d")
    return today_format

this_path = os.path.abspath('.')
excel_path = this_path + '\\2019年浦东实体门店和代理商月度评估.xlsm'

xl = Dispatch("Excel.Application")
xl.Visible = False #True是显示, False是隐藏
xl.DisplayAlerts = 0
xlBook = xl.Workbooks.Open(excel_path,False)
xs = xlBook.Sheets('文字结果')

doc = Document('2019年代理商评估具体分析.docx')
p = doc.add_paragraph()
e = doc.add_paragraph()
e.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT #右对齐
p.paragraph_format.line_spacing = 1.5 #行间距1.5

#设置每种文字的格式,style1是大标题格式,style2是小标题格式,style3是正文格式,因为add_style是在word里创建格式,所以第一次创建好后如果再次运行会报错,所以这里用了try except finally的方式
try:
    style1 = doc.styles['style head1']
except:
    style1 = doc.styles.add_style('style head1', 2)
finally:
    style1.font.bold = True
    style1.font.name = u'宋体 (中文正文)'
    style1.font.size = Pt(15)

try:
    style2 = doc.styles['style head2']
except:
    style2 = doc.styles.add_style('style head2', 2)
finally:
    style2.font.bold = True
    style2.font.name = u'宋体 (中文正文)'
    style2.font.size = Pt(12)

try:
    style3 = doc.styles['style head3']
except:
    style3 = doc.styles.add_style('style head3', 2)
finally:
    style3.font.name = u'宋体 (中文正文)'
    style3.font.size = Pt(10.5)

for i in range(2,14):
    name = xs.Cells(i, 1).Value
    score = str(float('%.1f' % xs.Cells(i, 2).Value))
    jjc_range = xs.Cells(i, 3).Value
    total_range = xs.Cells(i, 4).Value
    dianping = xs.Cells(i, 5).Value
    zyjjc_rank = chinese_rank(jjc_range)
    if total_range == 1:
        p.add_run(r'1、	大型代理商(千秋、外金、易贝)'+'\n',style = 'style head1')
    p.add_run(zyjjc_rank + name + '(' + score + '分)'+'\n', style = 'style head2')
    p.add_run(dianping +'\n', style = 'style head3')
    if total_range == 3:
        p.add_run(r'2、	中型代理商(共联、汉启、宁皙、君弘)'+'\n',style = 'style head1')
    if total_range == 7:
        p.add_run(r'3、	小型代理商(鹏德、仲诚、公惠、冠芝霖、茗神)'+'\n',style = 'style head1')
#最后的部门和日期用了单独的格式,和之前的正文区分开来
end_name = e.add_run('实体渠道运营中心'+'\n' + today_format())
xl.quit()
doc.save('2019年'+get_last_month() + u'代理商评估具体分析.docx')

最后成品的样子如下图所示:
在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42029733/article/details/86414985