教你使用Python根據(jù)模板批量生成docx文檔
一、需求說明
能夠根據(jù)模板批量生成docx文檔。具體而言,讀取excel中的數(shù)據(jù),然后使用python批量生成docx文檔。
二、實驗準備
準備excel數(shù)據(jù):
這里是關(guān)于學生語數(shù)英成績的統(tǒng)計表,文件名為score.xls
準備模板:
這是給學生家長的成績通知書,文件名為template.doc
另外,在使用python進行實驗之前,需要先安裝第三方庫docxtpl和xlrd,直接pip install就行:
pip install docxtpl pip install xlrd
然后將xls和doc和python文件放在同一個目錄下
三、代碼實現(xiàn)
首先打開xls,讀取數(shù)據(jù):
workbook = xlrd.open_workbook(sheet_path)
然后從文件中獲取第一個表格:
sheet = workbook.sheet_by_index(0)
然后遍歷表格的每一行,將數(shù)據(jù)存入字典列表:
tables = [] for num in range(1, sheet.nrows): stu = {} stu['name'] = sheet.cell_value(num, 0) stu['class'] = sheet.cell_value(num, 1) stu['language'] = sheet.cell_value(num, 2) stu['math'] = sheet.cell_value(num, 3) stu['English'] = sheet.cell_value(num, 4) tables.append(stu)
接下來將列表中的數(shù)據(jù)寫入docx文檔,其實這個過程可以在讀數(shù)據(jù)時同時進行,即讀完一行數(shù)據(jù),然后生成一個文檔。
首先在指定路徑生成一個docx文檔:
document = Document(word_path)
然后逐行進行正則表達式的替換:
paragraphs = document.paragraphs text = re.sub('name', stu['name'], paragraphs[1].text) paragraphs[1].text = text text = re.sub('name', stu['name'], paragraphs[2].text) text = re.sub('class', stu['class'], text) text = re.sub('language', str(stu['language']), text) text = re.sub('math', str(stu['math']), text) text = re.sub('English', str(stu['English']), text) paragraphs[2].text = text
其實不關(guān)心格式問題的,到現(xiàn)在為止就已經(jīng)結(jié)束了。但是這樣替換后docx中被替換的文字格式也被更改為系統(tǒng)默認的正文格式,所以接下來是將這些改成自己想要的格式:
遍歷需要更改格式的段落,然后更改字體大小和字體格式:
for run in paragraph.runs: run.font.size = Pt(16) run.font.name = "宋體" r = run._element.rPr.rFonts r.set(qn("w:eastAsia"), "宋體")
最后保存文件:
document.save(path + "\\" + r"{}的成績通知單.docx".format(stu['name']))
完整代碼:
from docxtpl import DocxTemplate import pandas as pd import os import xlrd path = os.getcwd() # 讀表格 sheet_path = path + "\score.xls" workbook = xlrd.open_workbook(sheet_path) sheet = workbook.sheet_by_index(0) tables = [] for num in range(1, sheet.nrows): stu = {} stu['name'] = sheet.cell_value(num, 0) stu['class'] = sheet.cell_value(num, 1) stu['language'] = sheet.cell_value(num, 2) stu['math'] = sheet.cell_value(num, 3) stu['English'] = sheet.cell_value(num, 4) tables.append(stu) print(tables) # 寫文檔 from docx import Document import re from docx.oxml.ns import qn from docx.shared import Cm,Pt for stu in tables: word_path = path + "\\template.doc" document = Document(word_path) paragraphs = document.paragraphs text = re.sub('name', stu['name'], paragraphs[1].text) paragraphs[1].text = text text = re.sub('name', stu['name'], paragraphs[2].text) text = re.sub('class', stu['class'], text) text = re.sub('language', str(stu['language']), text) text = re.sub('math', str(stu['math']), text) text = re.sub('English', str(stu['English']), text) paragraphs[2].text = text for paragraph in paragraphs[1:]: for run in paragraph.runs: run.font.size = Pt(16) run.font.name = "宋體" r = run._element.rPr.rFonts r.set(qn("w:eastAsia"), "宋體") document.save(path + "\\" + r"{}的成績通知單.docx".format(stu['name']))
四、實驗結(jié)果
文件中的文件:
生成的文件樣例:
到此這篇關(guān)于教你使用Python根據(jù)模板批量生成docx文檔的文章就介紹到這了,更多相關(guān)Python批量生成docx文檔內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python中常用的激活函數(shù)(Sigmoid、Tanh、ReLU等)
激活函數(shù) (Activation functions) 對于人工神經(jīng)網(wǎng)絡(luò)模型去學習、理解非常復(fù)雜和非線性的函數(shù)來說具有十分重要的作用,這篇文章主要介紹了Python中常用的激活函數(shù)(Sigmoid、Tanh、ReLU等),需要的朋友可以參考下2023-04-04Python利用prettytable實現(xiàn)格式化輸出內(nèi)容
Python有一個第三方模塊叫?prettytable,專門用來將數(shù)據(jù)格式輸出。本文將通過示例為大家詳細講講prettytable的用法,感興趣的可以了解一下2022-07-07Django用內(nèi)置方法實現(xiàn)簡單搜索功能的方法
這篇文章主要介紹了Django用內(nèi)置方法實現(xiàn)簡單搜索功能的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12python 實現(xiàn)的發(fā)送郵件模板【普通郵件、帶附件、帶圖片郵件】
這篇文章主要介紹了python 實現(xiàn)的發(fā)送郵件模板,包含Python發(fā)送普通郵件、帶附件及帶圖片郵件相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-07-07在前女友婚禮上用python把婚禮現(xiàn)場的WIFI名稱改成了
大家好,我是Lex 喜歡欺負超人那個Lex 擅長領(lǐng)域:python開發(fā),網(wǎng)絡(luò)安全滲透,Windows域控Exchange架構(gòu) 今日重點:python暴力拿下WiFi密碼;python拿下路由器管理頁面 代碼干貨滿滿,建議收藏+實操!有問題及需要,請留言哦2021-08-08