生活随笔
收集整理的這篇文章主要介紹了
期末了给孩子们一些鼓励吧!用Python批量制作【纸质】奖状的方法请查收!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原理:使用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
= nameself
.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
.pararun
= para
.add_run
(content
)run
.font
.size
= Pt
(fontsize
)run
.font
.name
= fontnamerun
.font
.underline
= underlinerun
.font
.bold
= boldr
= run
._element
.rPr
.rFontsr
.set(qn
('w:eastAsia'),fontname
)para
.alignment
= alignmentrun
.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
.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.合并的時候換頁如何換,有大佬能幫忙解答下嗎?
總結
以上是生活随笔為你收集整理的期末了给孩子们一些鼓励吧!用Python批量制作【纸质】奖状的方法请查收!的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。