校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > 教你使用Python根據模板批量生成docx文檔

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

熱門標簽:公司電話機器人 海南400電話如何申請 激戰2地圖標注 唐山智能外呼系統一般多少錢 陜西金融外呼系統 白銀外呼系統 哈爾濱ai外呼系統定制 廣告地圖標注app 騰訊外呼線路

一、需求說明

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

二、實驗準備

準備excel數據:

這里是關于學生語數英成績的統計表,文件名為score.xls

準備模板:

這是給學生家長的成績通知書,文件名為template.doc

另外,在使用python進行實驗之前,需要先安裝第三方庫docxtpl和xlrd,直接pip install就行:

pip install docxtpl
pip install xlrd

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

三、代碼實現

首先打開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)

接下來將列表中的數據寫入docx文檔,其實這個過程可以在讀數據時同時進行,即讀完一行數據,然后生成一個文檔。

首先在指定路徑生成一個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

其實不關心格式問題的,到現在為止就已經結束了。但是這樣替換后docx中被替換的文字格式也被更改為系統默認的正文格式,所以接下來是將這些改成自己想要的格式:

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

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根據模板批量生成docx文檔的文章就介紹到這了,更多相關Python批量生成docx文檔內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

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

標簽:鷹潭 惠州 黑龍江 四川 上海 益陽 黔西 常德

巨人網絡通訊聲明:本文標題《教你使用Python根據模板批量生成docx文檔》,本文關鍵詞  教你,使用,Python,根據,模板,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《教你使用Python根據模板批量生成docx文檔》相關的同類信息!
  • 本頁收集關于教你使用Python根據模板批量生成docx文檔的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 云阳县| 手机| 贵阳市| 左云县| 东至县| 衢州市| 富源县| 蒙山县| 佛学| 金坛市| 双牌县| 仙居县| 蓝山县| 蒙山县| 六安市| 东丽区| 阿鲁科尔沁旗| 枣强县| 新源县| 张家界市| 罗平县| 武城县| 大同县| 分宜县| 隆昌县| 伊金霍洛旗| 繁峙县| 庐江县| 偃师市| 江西省| 佛冈县| 扶沟县| 兴安县| 德兴市| 苏尼特右旗| 彩票| 长武县| 柘城县| 巴彦县| 大城县| 澄城县|