Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to generate docx documents in batches from templates

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Python to generate docx documents in batches according to the template", the content of the explanation in the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use Python to generate docx documents in batches according to templates"!

I. description of requirements

Be able to generate docx documents in batches based on templates. Specifically, read the data in excel and then use python to batch generate docx documents.

Second, experimental preparation

Prepare excel data:

Here is a statistical table about the students' scores in math and English. the file is called score.xls.

This is the grade notice for the parents of the students. The document is called template.doc.

In addition, before experimenting with python, you need to install the third-party libraries docxtpl and xlrd, and pip install directly:

Pip install docxtplpip install xlrd

Then put the xls and doc and python files in the same directory

Third, code implementation

First, open xls and read the data:

Workbook = xlrd.open_workbook (sheet_path)

Then get the first table from the file:

Sheet = workbook.sheet_by_index (0)

Then iterate through each row of the table and store the data in the dictionary list:

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)

Next, write the data in the list to the docx document, which can be done at the same time as you read the data, that is, read a line of data and then generate a document.

First generate an docx document in the specified path:

Document = Document (word_path)

Then replace the regular expression line by line:

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

As a matter of fact, those who don't care about the format are over by now. But after this replacement, the text format replaced in docx is also changed to the default body format of the system, so the next step is to change these to the format you want:

Traverse paragraphs that need to be formatted, and then change the font size and font format:

For run in paragraph.runs: run.font.size = Pt (16) run.font.name = "Song style" r = run._element.rPr.rFonts r.set (qn ("w:eastAsia"), "Song style")

Finally, save the file:

Document.save (path + "\" + r "{} transcript .docx" .format (stu ['name']))

Complete code:

From docxtpl import DocxTemplateimport pandas as pdimport osimport xlrdpath = os.getcwd () # read form 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) # write document from docx import Documentimport refrom docx.oxml.ns import qnfrom docx.shared import Cm,Ptfor 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 = "Song style" r = run._element.rPr.rFonts r.set (qn ("w:eastAsia") Document.save (path + "\" + r "{} report note .docx" .format (stu ['name'])) Thank you for your reading These are the contents of "how to use Python to generate docx documents in batches according to the template". After the study of this article, I believe you have a deeper understanding of how to use Python to generate docx documents in batches according to the template. the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report