python3 荣誉证书(奖状)批量打印
生活随笔
收集整理的這篇文章主要介紹了
python3 荣誉证书(奖状)批量打印
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、功能介紹圖、與插件界面
?
二、說明
功能介紹:
?? ?批量讀取excel中的姓名,項目名稱,批量渲染word文檔并進行打印。
?? ?適用于各類獎狀,榮譽證書的打印工作等。
1、系統要求windos10(7) 64位,并安裝office2007以上版本
2、設備連接打印機
3、excel數據表格(2007以上版本,文件后綴為.xlsx):
?? ?第一列為姓名,第二列為項目(不帶表頭)
4、word打印模板(2007以上版本,文件后綴為docx):
?? ?a、對打印模板進行設計
?? ?b、使用{{name}}占位名字的位置
?? ?c、使用{{subject}}占位項目名稱的位置
?? ?特別提示:占位符請設置好字體等格式
6、運行AutoPrint.exe進入自渲染批量打印:
?? ?1、選擇Excel數據表格
?? ?2、選擇Word模板文檔
?? ?3、點擊打印
7、具體excel數據表格、word模板文件可參考【data.xlsx】、【model.docx】
三、源碼
第三方庫pypiwin32、pfExcel(基于openpyxl封裝便于更好的操作excel文件,博客中有源碼不再列舉。
利用tkinter做了一個簡單的頁面
(使用pyinstaller可編譯為exe)
import os from threading import Thread import win32com.client from docxtpl import DocxTemplate from tkinter import Tk, Button, filedialog, messagebox, StringVar from pfExcel import ExcelWorkclass AP(Tk):def __init__(self):super().__init__()# 選擇Excel表格按鈕字符串self.bt_excel = StringVar()self.bt_excel.set('請選擇Excel表格')# 選擇Word模板按鈕字符串self.bt_word = StringVar()self.bt_word.set('請選擇Word模板')# 打印按鈕字符串self.bt_print = StringVar()self.bt_print.set('打印')# Excel表格地址self.excelPath = ''# Word模板地址self.wordPath = ''# UI初始化self.uiInit()self.mainloop()def uiInit(self):# 屏幕寬度sw = self.winfo_screenwidth()# 屏幕高度sh = self.winfo_screenheight()# 窗口大小ww = 280wh = 180# 計算顯示位置(垂直水平居中)x = (sw - ww) / 2y = (sh - wh) / 2self.geometry("%dx%d+%d+%d" % (ww, wh, x, y))# 標題欄self.title('自渲染批量打印')# 圖標self.iconbitmap('logo.ico')# 禁止縮放self.resizable(0, 0)# 選擇Excel按鈕Button(self,width=22,height=1,textvariable=self.bt_excel,font=('', 14),fg='green',command=self.getExcelPath).pack(pady=20)# 選擇Word按鈕Button(self,width=22,height=1,textvariable=self.bt_word,font=('', 14),fg='blue',command=self.getWordPath).pack(pady=10)# 打印按鈕Button(self,width=6,textvariable=self.bt_print,font=('', 12),command=self.autoPrintDom).pack(pady=10)def getExcelPath(self):"""獲取excel表格地址:return:"""self.excelPath = filedialog.askopenfilename()# 判斷地址是否為空if self.excelPath:# 判斷后綴if self.excelPath.split('.')[1] == 'xlsx':messagebox.showinfo('提示', '選擇成功')self.bt_excel.set('Excel表格已選中')else:messagebox.showinfo('提示', '請選擇后綴名為xlsx的文件')else:messagebox.showinfo('提示', '選擇失敗')def getWordPath(self):"""獲取Word模板地址:return:"""self.wordPath = filedialog.askopenfilename()# 判斷地址是否為空if self.wordPath:# 判斷后綴if self.wordPath.split('.')[1] == 'docx':messagebox.showinfo('提示', '選擇成功')self.bt_word.set('Word模板已選中')else:messagebox.showinfo('提示', '請選擇后綴名為docx的文件')else:messagebox.showinfo('提示', '選擇失敗')def autoPrintDom(self):"""關聯打印按鈕:return:"""if self.wordPath != '' and self.excelPath != '':if self.bt_print.get() == '打印':self.bt_print.set('取消')hd = Thread(target=self.handle)# 子線程跟隨主進程關閉hd.daemon = Truehd.start()else:self.bt_print.set('打印')else:messagebox.showinfo('提示', '請選擇Excel與Word')def handle(self):"""打印處理線程:return:"""# 讀取excel數據excelData = self.readExcel(self.excelPath)for name, subject in excelData:# 設置按鈕顯示日志# 渲染word模板ct = {'name': name,'subject': subject}self.renderWord(self.wordPath, 'd:\\temp.docx', ct)self.bt_excel.set(name + '->渲染完成')self.printWord('d:\\temp.docx')os.remove('d:\\temp.docx')self.bt_word.set(name + '->已加入打印序列')if self.bt_print.get() == '打印':# 初始化按鈕字符串self.bt_excel.set('請選擇Excel表格')self.bt_word.set('請選擇Word模板')breakmessagebox.showinfo('提示', '已全部加入打印序列')# 初始化按鈕字符串self.bt_print.set('打印')self.bt_excel.set('請選擇Excel表格')self.bt_word.set('請選擇Word模板')@staticmethoddef readExcel(excelFilePath):"""讀取excel表格:param excelFilePath: excel文件路徑:return: [[行], ]"""excel = ExcelWork(excelFilePath)data = []for index, var in enumerate(excel.getColumn(1)):data.append(excel.getRow(index + 1))excel.close()return data@staticmethoddef renderWord(wordFilePath, newWordFilePath, cont):"""渲染word模板:param wordFilePath: word文件路徑:param newWordFilePath: 新文件路徑:param cont: {渲染數據}:return:"""tpl = DocxTemplate(wordFilePath)tpl.render(cont)tpl.save(newWordFilePath)@staticmethoddef printWord(wordFilePath):"""打印word文件:param wordFilePath: word文件路徑:return:"""tool = 'Word.Application'# WPS tool = 'wps.application'word = win32com.client.Dispatch(tool)# 在后臺運行程序word.Visible = 0 # 后臺運行,不顯示# 運行過程不警告word.DisplayAlerts = 0 # 不警告# 打開word文檔doc = word.Documents.Open(wordFilePath)# 進行打印doc.PrintOut()doc.Close()word.Quit()if __name__ == '__main__':AP()?
總結
以上是生活随笔為你收集整理的python3 荣誉证书(奖状)批量打印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python_072205_创建一个类方
- 下一篇: win7系统efi激活教程:无需efi