原理:使用python-docx模块制作奖状生成器,批量生成word奖状。由于每个学生拥有一个word文档,打印起来非常不方便,所以再利用文档合并代码,将所有奖状的word放在一个word里就可以批量打印奖状了。
一、安装三个库
pip install python-docx
pin install pinyin
pip install certimaker
我在安装docx库时报错,于是采用了国内安装源:
pip install python-docx -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
即可安装成功:
二、制作单个奖状代码
"""
制作纸质奖状
"""
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
from docx import Document
import pinyin
import datetime
from docxclass import make_certificate
"""
设置文本颜色为黑色
"""
BLACK = RGBColor(0,0,0)
"""
获取当前时间以便落款
"""
THISYM = datetime.datetime.now().strftime('%Y年%m月')
"""
绘制word内容
"""
class DocWriter:
def __init__(self,name,award):
self.doc = Document()
self.name = name
self.award = award
def new_para(self):
self.para = self.doc.add_paragraph()
def write_run(self, content, fontsize = 25, fontname = '宋体',
alignment = WD_ALIGN_PARAGRAPH.LEFT, color = BLACK,
underline = False, bold = False):
para = self.para
run = para.add_run(content)
run.font.size = Pt(fontsize)
run.font.name = fontname
run.font.underline = underline
run.font.bold = bold
r = run._element.rPr.rFonts
r.set(qn('w:eastAsia'),fontname)
para.alignment = alignment
run.font.color.rgb = color
def signature(self,sign):
self.new_para()
self.write_run(sign,alignment=WD_ALIGN_PARAGRAPH.RIGHT,fontsize = 20,bold=True)
self.new_para()
self.write_run(THISYM,alignment=WD_ALIGN_PARAGRAPH.RIGHT,fontsize = 20,bold=True)
def save_doc(self):
pyname = pinyin.get(self.name, format='strip', delimiter="")
pyaward= pinyin.get(self.award,format='strip', delimiter="")
filename = pyname + '_'+ pyaward + '.docx'
self.doc.save(filename)
"""
制作奖状单个奖状
"""
def make_certificate(name,award):
awardoc = DocWriter(name,award)
awardoc.new_para()
awardoc.new_para()
awardoc.new_para()
awardoc.new_para()
awardoc.new_para()
awardoc.new_para()
awardoc.write_run('\t'*2+name,fontsize = 25,underline = False,bold=True)
awardoc.write_run('同学:',fontsize = 20,bold=True)
awardoc.new_para()
awardoc.write_run(f'\t\t\t荣……获第三届科技节编程类竞赛',underline = False,bold=True,fontsize = 25)
awardoc.new_para()
awardoc.write_run('\t'*8 + award + ' '*2, underline=False, bold=True,fontsize = 40)
awardoc.new_para()
awardoc.signature('……小学校')
awardoc.save_doc()
name = '羊羊羊'
award = '一等奖'
make_certificate(name,award)
三、批量制作奖状
在字典中添加想要制作的奖状即可批量生产。
award_dict = {
'羊羊羊':'一等奖',
……
}
for name,award in award_dict.items():
make_certificate(name,award)
四、合并所有奖状方便打印
因为批量生产出来的奖状如下,不方便打印
于是,我们来合并所有word文件吧!!!
"""
合并奖状
"""
import win32com.client as win32
import os
word = win32.gencache.EnsureDispatch('Word.Application')
#启动word对象应用
word.Visible = False
path = r'C:/Users/86136/Desktop/奖状制作/制作纸质奖状'
files = []
for filename in os.listdir(path):
filename = os.path.join(path,filename)
files.append(filename)
#新建合并后的文档
output = word.Documents.Add()
for file in files:
output.Application.Selection.InsertFile(file)#拼接文档
#获取合并后文档的内容
doc = output.Range(output.Content.Start, output.Content.End)
output.SaveAs('C:/Users/86136/Desktop/奖状制作/合并奖状.docx') #保存
output.Close()
五、问题
1.怎么初始化页面设置为横向+A4纸,有大佬能帮忙解答下吗?
2.合并的时候换页如何换,有大佬能帮忙解答下吗?