用python自动生成.docx格式的邀请信提高10倍以上办公效率

完成这个任务,我们需要有以下预备工作:
1.安装python-docx库
2.了解docx的doc/paragraph/run层次模型
3.字符串的处理方法
4.使用内置模块调用当前日期并且转换成字符串

from docx import Document
from docx.shared import Pt, RGBColor #字体大小和颜色
from docx.enum.text import WD_ALIGN_PARAGRAPH #调整段落居中
import datetime
guest_name = 'kobe bryant'
doc = Document()
header = doc.add_paragraph()
header_run = header.add_run('INVITATION')
header_run.font.bold = True
header_run.font.size = Pt(30)
header.alignment = WD_ALIGN_PARAGRAPH.CENTER

title = doc.add_paragraph()
title_run = title.add_run(guest_name.title()+' :')
title_run.font.size = Pt(20)
title_run.font.name = 'Segoe Script'
content = doc.add_paragraph()
content_run = content.add_run("Welcome to my party that will begin at 17:00 on April 1st:"\
"It will take place at No.123 Nanjing Road, Shanghai. I would really like you could"\
"come and enjoy the time with us." #字符串太长,使用\来连接多行
)
content_run.font.size = Pt(12)
content_run.font.name = 'Segoe Script'
content.paragraph_format.line_spacing = 2.0
signature = doc.add_paragraph()
signature_run = signature.add_run('Dr.Wu\n')
signature_run.font.name = 'Segoe  Script'
signature_run.font.size = Pt(18)
now_time = datetime.datetime.now().strftime('%Y-%m-%d')
sign_time_run = signature.add_run(now_time)
sign_time_run.font.size = Pt(12)
sign_time_run.font.name = 'Segoe  Script'
signature.alignment = WD_ALIGN_PARAGRAPH.RIGHT
doc.save('invitation_to_'+guest_name+'.docx')

运行之后我们就可以自动生成邀请函word文档,打开以后是这个样子的:
在这里插入图片描述
以后需要批量制作的时候,就只要在python中修改guest_name就可以了,不需要无聊地打开复制粘贴、修改、保存了,工作效率提高10倍以上。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值