Automating Word Documents from Excel Using Python | ‘docxtpl’ Tutorial


    Did you know you can use Python code to dynamically create Word Documents? For this tutorial, we will be using the Python libraries ‘xlwings’ & ‘docxtpl’ to automate Word. In particular, we will be writing values from Excel to Word by using Python.

    Download all the files from the tutorial:
    👉 GitHub:

    🌎 Website:
    📝 GitHub:
    ⭐ Discord:
    ▶️ Subscribe:

    🎉 𝗙𝗥𝗘𝗘 𝗘𝘅𝗰𝗲𝗹 𝗔𝗱𝗱-𝗶𝗻 𝘁𝗼 𝗯𝗼𝗼𝘀𝘁 𝘆𝗼𝘂𝗿 𝗽𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝘃𝗶𝘁𝘆
    Get it here:

    📚 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗳𝗼𝗿 𝗹𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗩𝗕𝗔 & 𝗣𝘆𝘁𝗵𝗼𝗻
    Check out my recommendations:

    ☕ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲❓
    If you want to support this channel, you can buy me a coffee here:


    Previous articleBeginner Tutorials For Boto3 in hindi – What is Boto3 – AWS python sdk Boto3 tutorials
    Next articleMapReduce | MapReduce in Hadoop | Hadoop Tutorial for Beginners | Hadoop [Part 10]


    1. Hi! Many thanks for this wonderful tutorial. Appreciate it. I was able to follow your instructions and design one. It worked. But I have one minor issue. I have a date cell in Excel. I want to populate word with string date for example 1 January, 2022 instead of 1/1/2022. Is there a way out? Thanks once again and have a nice day!😊

    2. Is It possible to get the data from Multiple Columns and Multiple Rows ?
      for example Col A : Company Name, Col B : Cities, Col C : Revenue and so on.
      The Word Document will have 3 paragraph, Each Paragrah will talk about a different company from the above table.

      After testing this, I've met some problems:

      Traceback (most recent call last):

      File "", line 9, in <module>

      placeholder_1 = InlineImage(doc, 'Placeholders/Placeholder_1.png', Cm(5))

      NameError: name 'InlineImage' is not defined

    3. Thanks for the great tutorial. I am actually trying to create a very similar system. I have a question regarding the the line: import xlwings as xw and the line: from docxtpl import DocxTemplate. I get the errors Import "xlwings" could not be resolved and Import "docxtpl" could not be resolved. I checked and both libraries are installed. Do you have an idea what might be going wrong in this case? I used exactly the same code as in your video. I am using a mac though.

    4. —————————



      Traceback (most recent call last):

      File "<string>", line 1, in <module>

      File "", line 7, in <module>


      OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ''

      Press Ctrl+C to copy this message to the clipboard.

      What should I do now? Thanks in advance!

    5. Hello, thank you very much for your very useful video.
      I would like to reproduce the same thing but instead of generating all the pdfs at the same time I want the word file to be created on demand.
      For example, I want to have a drop-down list that will allow me to select a client and then the code will generate its word file with its information.
      The drop-down list can be on Excel or on another interface. Do you have any ideas on how to do this please?

    6. Thank you so much for this awesome content. The first part works for me, I am able to render the file based on the template. But when I try to do it using the button on Excel I get this error —————————
      Package not found at 'MainTemplate.docx'
      I really do not understand as anyone ran into a similar issue?
      Thank you for the tutorial!

    7. Hi, this is exactly what I've been looking for. However, I can't seem to get it to run, even though I follow you to each steps. Maybe because I'm using a Mac? 

      I'd love to if you could give me some suggestions

      Here is the error code when I'm running the basic example:

      Traceback (most recent call last):
      File "", line 16, in <module>
      File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/docxtpl/", line 317, in render
      xml_src = self.build_xml(context, jinja_env)
      File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/docxtpl/", line 272, in build_xml
      xml = self.render_xml_part(xml, self.docx._part, context, jinja_env)
      File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/docxtpl/", line 227, in render_xml_part
      raise exc
      File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/docxtpl/", line 220, in render_xml_part
      template = Template(src_xml)
      File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/", line 1031, in _new_
      return env.from_string(source, template_class=cls)
      File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/", line 941, in from_string
      return cls.from_code(self, self.compile(source), globals, None)
      File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/", line 638, in compile
      File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/", line 832, in handle_exception
      File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/", line 28, in reraise
      raise value.with_traceback(tb)
      File "<unknown>", line 1, in template
      jinja2.exceptions.TemplateSyntaxError: unexpected char '(' at 4984
      (base) [email protected] word_automation %