濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 教你使用Python根據(jù)模板批量生成docx文檔

教你使用Python根據(jù)模板批量生成docx文檔

熱門(mén)標(biāo)簽:公司電話(huà)機(jī)器人 海南400電話(huà)如何申請(qǐng) 激戰(zhàn)2地圖標(biāo)注 唐山智能外呼系統(tǒng)一般多少錢(qián) 陜西金融外呼系統(tǒng) 白銀外呼系統(tǒng) 哈爾濱ai外呼系統(tǒng)定制 廣告地圖標(biāo)注app 騰訊外呼線(xiàn)路

一、需求說(shuō)明

能夠根據(jù)模板批量生成docx文檔。具體而言,讀取excel中的數(shù)據(jù),然后使用python批量生成docx文檔。

二、實(shí)驗(yàn)準(zhǔn)備

準(zhǔn)備excel數(shù)據(jù):

這里是關(guān)于學(xué)生語(yǔ)數(shù)英成績(jī)的統(tǒng)計(jì)表,文件名為score.xls

準(zhǔn)備模板:

這是給學(xué)生家長(zhǎng)的成績(jī)通知書(shū),文件名為template.doc

另外,在使用python進(jìn)行實(shí)驗(yàn)之前,需要先安裝第三方庫(kù)docxtpl和xlrd,直接pip install就行:

pip install docxtpl
pip install xlrd

然后將xls和doc和python文件放在同一個(gè)目錄下

三、代碼實(shí)現(xiàn)

首先打開(kāi)xls,讀取數(shù)據(jù):

workbook = xlrd.open_workbook(sheet_path)

然后從文件中獲取第一個(gè)表格:

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)

接下來(lái)將列表中的數(shù)據(jù)寫(xiě)入docx文檔,其實(shí)這個(gè)過(guò)程可以在讀數(shù)據(jù)時(shí)同時(shí)進(jìn)行,即讀完一行數(shù)據(jù),然后生成一個(gè)文檔。

首先在指定路徑生成一個(gè)docx文檔:

document = Document(word_path)

然后逐行進(jìn)行正則表達(dá)式的替換:

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

其實(shí)不關(guān)心格式問(wèn)題的,到現(xiàn)在為止就已經(jīng)結(jié)束了。但是這樣替換后docx中被替換的文字格式也被更改為系統(tǒng)默認(rèn)的正文格式,所以接下來(lái)是將這些改成自己想要的格式:

遍歷需要更改格式的段落,然后更改字體大小和字體格式:

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"{}的成績(jī)通知單.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)
 
# 寫(xiě)文檔
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"{}的成績(jī)通知單.docx".format(stu['name']))

四、實(shí)驗(yàn)結(jié)果

文件中的文件:

生成的文件樣例:

到此這篇關(guān)于教你使用Python根據(jù)模板批量生成docx文檔的文章就介紹到這了,更多相關(guān)Python批量生成docx文檔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 使用python批量生成insert語(yǔ)句的方法
  • python基礎(chǔ)學(xué)習(xí)之生成器與文件系統(tǒng)知識(shí)總結(jié)
  • python生成器generator:深度學(xué)習(xí)讀取batch圖片的操作
  • 教你怎么用Python生成九宮格照片
  • Python如何生成隨機(jī)高斯模糊圖片詳解
  • python使用ProjectQ生成量子算法指令集
  • Python實(shí)現(xiàn)K-means聚類(lèi)算法并可視化生成動(dòng)圖步驟詳解
  • python基于opencv批量生成驗(yàn)證碼的示例
  • 用python自動(dòng)生成日歷
  • Python趣味挑戰(zhàn)之給幼兒園弟弟生成1000道算術(shù)題

標(biāo)簽:鷹潭 惠州 黑龍江 四川 上海 益陽(yáng) 黔西 常德

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《教你使用Python根據(jù)模板批量生成docx文檔》,本文關(guān)鍵詞  教你,使用,Python,根據(jù),模板,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《教你使用Python根據(jù)模板批量生成docx文檔》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于教你使用Python根據(jù)模板批量生成docx文檔的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    牙克石市| 陆川县| 遂宁市| 壤塘县| 江陵县| 太仓市| 安塞县| 鹤峰县| 武邑县| 铜川市| 新沂市| 古蔺县| 张北县| 崇阳县| 石渠县| 吴堡县| 体育| 太仓市| 郎溪县| 天全县| 永仁县| 香格里拉县| 绥棱县| 泌阳县| 成武县| 修武县| 齐齐哈尔市| 鹰潭市| 东乌珠穆沁旗| 太湖县| 和田市| 大理市| 商河县| 巴林右旗| 舟曲县| 梅州市| 库尔勒市| 鹤岗市| 罗山县| 天峨县| 聂荣县|