Update: The following modules have been uploaded to pypi and can be used directly.
pip install certimaker
Of course, this program requires two pre-installed packages. If you have not installed the python-docx module and pinyin module, you can use the following code to install these two modules:
pip install python-docx
pin install pinyin
Download the certimaker module, and then use the following code in the local code to quickly generate a certificate
from certimaker.certimaker import make_certificate
make_certificate('王大雷','国家奖学金')
To complete this small project, you need to master the following knowledge points:
1. The import method of the third-party module
2. The docx model of the word document (doc / paragraph / run three-layer structure)
3. The basis of class and function writing (OOP)
4. Basic usage of datetime module
code show as below:
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
import pinyin
import datetime
BLACK = RGBColor(0,0,0)
RED = RGBColor(255,0,0)
GREEN = RGBColor(0,255,0)
BLUE = RGBColor(0,0,255)
TODATE = datetime.datetime.now().strftime('%Y.%m.%d')
THISY = datetime.datetime.now().strftime('%Y')
LASTY = str(int(THISY)-1)
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)
self.new_para()
self.write_run(TODATE,alignment=WD_ALIGN_PARAGRAPH.RIGHT)
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.write_run('奖 状', fontsize = 50,alignment= WD_ALIGN_PARAGRAPH.CENTER,color = RED,bold=True)
awardoc.new_para()
awardoc.write_run(' '*2+name+' '*2,fontsize = 30,underline = True,bold=True)
awardoc.write_run(' 同学:',fontsize = 30)
awardoc.new_para()
awardoc.write_run(f'\t你在{LASTY}—{THISY}年度表现优异,被授予')
awardoc.write_run(' '*2 + award + ' '*2, underline=True, bold=True)
awardoc.write_run('荣誉称号。')
awardoc.new_para()
awardoc.write_run('\t特发此证,以资鼓励!')
awardoc.signature('python大学')
awardoc.save_doc()
name = '李雷'
award = '三好学生'
make_certificate(name,award)
After running, the .docx type word document will be generated as follows:
After opening, we can see that a certificate of success has been generated:
We save the above code as the module 'docxclass.py', we can call the function make_certificate () defined in this module in other python code, we create a new python file and write the following code:
from docxclass import make_certificate
award_dict = {
'李雷':'三好学生',
'韩梅梅':'学习积极分子',
'Jim':'体育积极分子',
'Kate':'文艺积极分子',
'Lucy':'劳动积极分子',
'Lily':'德育积极分子',
}
for name,award in award_dict.items():
make_certificate(name,award)
This allows you to generate certificates in batches:
We can open one of them at will to get the desired certificate effect: