您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何使用Python根據(jù)模板批量生成docx文檔”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何使用Python根據(jù)模板批量生成docx文檔”吧!
能夠根據(jù)模板批量生成docx文檔。具體而言,讀取excel中的數(shù)據(jù),然后使用python批量生成docx文檔。
準備excel數(shù)據(jù):
這里是關(guān)于學(xué)生語數(shù)英成績的統(tǒng)計表,文件名為score.xls
這是給學(xué)生家長的成績通知書,文件名為template.doc
另外,在使用python進行實驗之前,需要先安裝第三方庫docxtpl和xlrd,直接pip install就行:
pip install docxtpl pip install xlrd
然后將xls和doc和python文件放在同一個目錄下
首先打開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']))
感謝各位的閱讀,以上就是“如何使用Python根據(jù)模板批量生成docx文檔”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何使用Python根據(jù)模板批量生成docx文檔這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。