Use docxtpl library to realize automatic output of docx report

For many auditors, it is common to work overtime to make drafts, but the most troublesome thing is not making drafts, but revising reports.
Generally speaking, a report corresponds to an audit report. If a certain number in the report is changed, the numbers in the entire report will change, and all the numbers in the entire report will have to be revised.
The technology back then was limited, so we had no choice but to slowly change it.
Now I finally learned it, but I no longer do audits.
I wrote a small program, I hope it can help newcomers.
This is a relatively unpopular Python library, and there are not many functions in the library, only a few, but its functions are good. If combined with the Pandas library, the data on the Excel table can be automatically converted into word labels. If the power of the docx library lies in generating the Word documents we want, then the existence of the Docxtpl library is to output these documents in a fixed format.
Installation method is as follows:

After pip install docxtpl
is installed successfully, it does not mean that it can be used immediately. You need to set the label of the word template before entering the code.
What is a Word template? In fact, it is just a word file, but this file is a little special:

The double brackets in the Word document are tags. It is a variable, which can be either Zhang San or Li Si. Tags can only be used within the same paragraph of the same paragraph and cannot be used across multiple paragraphs, table rows, or paragraphs. And leave a space on both sides of the double brackets.
After setting the labels, set each column of the Excel document:


The header of each column must be consistent with the name of the Word document tag. Only in this way can the data in Excel be labeled and the content under the header be placed in the correct location in Word.
After Word and Excel are set up, let's enter the code:

    import pandas as pd
    from docxtpl import DocxTemplate
    df01=pd.read_excel("Import data.xlsx") #Import the table, the partial code is simplified
    df02 = df01.to_dict(orient="records") ##Convert the data into a dictionary
    for i in df02:
            tpl = DocxTemplate('Internal audit report template 03.docx') ##Import the Word document
            tpl.render(i) ##Replace the dictionary with the Word tag
            tpl.save(i['Company Name']+'.docx' ) ##Save Word documents by company
The code is very simple. To be precise, there are only three functions:
The function of the DocxTemplate() function is to import the Word document. If it is not in the same folder, enter the absolute path. Secondly, the function of .render () function is to replace Word tags. Finally, the .save() function generates several word documents.


If the Word document is designed to be more detailed, the label can be designed to be more detailed:

A more detailed report will then be generated.

For many people who have to work overtime because they are rushing to report, this kind of small program greatly saves time. As long as the template and data are prepared in advance, the report can be generated with one click.
In addition to automatically generating reports, payroll can also be automatically generated. Just imagine, if you import the salary details into a Word document, everyone's payslip will be automatically generated after running it. Then the accountant does not need to use scissors to cut the salary slip into strips every month, but can directly send each person's salary slip to each person's WeChat ID.
If it is a school, it can also automatically generate admission notices and the like. As long as you make an Excel table, you can generate admission notices for each person based on the list.

Guess you like

Origin blog.csdn.net/xifenglie123321/article/details/131986811