How to create .doc files using templates using django / python

I am writing a django application, and there is something that I do not know how to do. Let's say you have a database with users and several .doc files that you can send to these users (mail letters, not electronic ones). I am wondering if there is a way to automatically create these letters from patterns using my user database to fill in some markers? (e.g. name, address, etc.)

I heard about the python library "pod", but I never used it. It seems that the documentation uses openoffice templates, but mine use MS Word format. Since they are often updated by people who use MS Office, I'm stuck.

Any help would be appreciated.

J.

+3
source share
2 answers

There are many different ways to do this, depending on the context. Here are some ideas ranked in decreasing order of difficulty.

  • Microsoft Word includes a tool for this, called merging. You can control Word from Python using COM interceptors by installing pywin32. This will do the same as calling Merge by opening Microsoft Word. This needs to be done on a Windows computer with Office installed, perhaps not on your server. See http://bytes.com/topic/python/answers/165364-ms-word-mail-merge-automation or Google "merging python letters".

  • OpenOffice Python ( ) API Python-UNO. OpenOffice , pod .

  • , , OpenOffice.

  • Django, , , .

, .

+3

mailmerge , doc.

http://pbpython.com/python-word-template.html

. , my_template1 Word, name title.

def TestDocument2(request):
    template = os.path.join(os.path.dirname(__file__), 
    'templates/my_template1.docx')  

    document = MailMerge(template)
    document.merge(name='testcoy',
               title = 'My title',
               )
    f = io.BytesIO()
    document.write(f)
    length = f.tell()
    f.seek(0)
    response = HttpResponse(
        f.getvalue(),
        content_type='application/vnd.openxmlformats-
        officedocument.wordprocessingml.document'
         )
    response['Content-Disposition'] = 'attachment; filename=example.docx'
    response['Content-Length'] = length
    return response
0

Source: https://habr.com/ru/post/1760522/


All Articles