Fill out documents in Microsoft Word using Python. Part 1

    We fulfill the obligation to obtain information about our beneficial owners


    Small introductory


    Beginning December 21, 2016, amendments to the Federal Law of the Russian Federation “On combating the legalization (laundering) of proceeds of crime and the financing of terrorism” came into force, regarding the obligation of a legal entity to disclose information about its beneficial owners. In this regard, many companies send tenure requests in order to ascertain their beneficial owners. Someone makes requests on paper, someone sends out emails.

    In our opinion, the proper proof of fulfillment of the “Know Your Beneficial Owner” obligation is the presence of a letter on paper with a note about sending / delivery. These letters should ideally be prepared at least once a year. If the lawyer has only a few companies, writing letters is not difficult. But, if there are more than 3 dozens of companies, writing letters turns into a destructive routine. The matter is aggravated by the fact that the details of letters are constantly changing: signatories quit, companies re-register, changing addresses. All this must be taken into account. How can python programming skills help here?

    It is very simple - it would be nice to have a program that itself will substitute the necessary details in the letters. Including to form letters themselves, without forcing to create a document after a document manually. Let's try it.

    The structure of the letter in word. Python docxtpl module


    Before writing the program code, let's see what the letter template should look like in which we will place our data.

    The text of the letter from the company to its participant / shareholder will be approximately as follows:



    We will write a simple program that will fill in one field in our template to begin with in order to understand the principle of work.

    To begin with, in the Word letter template itself, instead of one of the fields, for example, a signer, we will set a variable. This variable must be in either English. language, or in Russian, but in one word. Also, the variable must be necessarily enclosed in double curly brackets. It will look something like this:



    The program itself will look like this:

    from docxtpl import DocxTemplate
    doc = DocxTemplate("шаблон.docx")
    context = { 'director' : "И.И.Иванов"}
    doc.render(context)
    doc.save("шаблон-final.docx")
    

    First, we import a module for working with Word format documents. Next, we open the template, and in the director field, which would have been indicated earlier in the template itself, enter the name of the director. At the end, the document is saved under a new name.

    Thus, in order to fill in all the fields in the Word template file, we first need to define all the input fields in the template itself with brackets {} along with the variables and then write a program. The code will be something like this:

    from docxtpl import DocxTemplate
    doc = DocxTemplate("шаблон.docx")
    context = { 'emitent' : 'ООО Ромашка', 'address1' : 'г. Москва, ул. Долгоруковская, д. 0', 'участник': 'ООО Участник', 'адрес_участника': 'г. Москва, ул. Полевая, д. 0', 'director': 'И.И. Иванов'}
    doc.render(context)
    doc.save("шаблон-final.docx")
    

    At the exit, when executing the program, we will receive a completed document.

    You can download the finished Word template here .

    Also popular now: