Menu Close
    Hire us Request quote Reading list Switch to dark mode

    Create .docx file in python(Odoo): python-docx

    In this tutorial, I will discuss how to create the .docx file using python. In python one of the ways to create the .docx file is using the pythondocx library. Recently I was working on a project in Odoo where I needed to create a .docx file using python. I faced some challenges that I am sharing here.  I will show how to add the images, header, footer, etc in the .docx file.

    In order to start with the python-docx, you have to install the library, you can install the library using any of the following ways.
    1) You can install it using pip as:


    2)You can install it using easy_install as:


    3) You can also install it by manually downloading from PyPI, unpack it and then simply run setup.py


    After the library is installed you can create a .docx file. I will create a “Test_word.docx” file and I will explain the meaning of some words also.

    This is the screenshot of the file that has been created after compilation.

    You can customize your file according to your requirement using the style, fonts, width, alignment, etc. You can check the features from the GitHub.
    https://github.com/python-openxml/python-docx

    If you are using Odoo then you can also save this file in the attachment and then can download it from there whenever required.


    That is it.!!!
    If you liked this post, I would be very grateful if you write your opinions, comments and suggestions to keep the post updated and interesting.
    Thank you!

    Current Product Version - 1.0.0

    . . .

    Comments (9)

    Add Your Comment

  • Emilio
    Hi. Best compliment for your script!
    For you….Is possible add a string for automatic import of all picture are in a specificy directory…resize…and insert they automatically in a table with two row and One colomn for any picture?
    • Jahangir Naik
      Hello Emillo,
      I didn’t get your query properly.
      For resizing you can use like this
      from docx import Document
      from docx.shared import Inches

      doc = Document(‘addImage.docx’)
      tables = doc.tables
      p = tables[0].rows[0].cells[0].add_paragraph()
      r = p.add_run()
      r.add_picture(‘resized.png’,width=Inches(4.0), height=Inches(.7))
      p = tables[1].rows[0].cells[0].add_paragraph()
      r = p.add_run()
      r.add_picture(‘teste.png’,width=Inches(4.0), height=Inches(.7))
      doc.save(‘addImage.docx’)

      You can explain this query in detail by writing an email at support@webkul.com and I will try my level best to answer your query.

      • Emilio
        Dear Jahangir Naik,
        Thanks you for your very fast answer
        I explane my problem.

        I Need this kind of script

        In a specificy directory i put a lot of picture (.jpg…..PNG, etc)

        The script must:

        Create a document docx and, for every picture create a single tablet with

        One column and 5 row with

        1. Image preview resized (2 picture/table for Page)

        2. Name file

        3. Date time original file

        4. Hash md5 (if Is possibile).

        5. Blank

        What do you think about project?

        I’m very principiant in python and it’s not a simple script for me.

        If you want i can create the file and send to you.

        Thank you very much for all you answer.

        Best regards

  • Divyansh Tripathi
    Hey, thanks for the article but I was wondering if there any way to add the z-index to the image in python docx because I want to add the picture background in the word document. Any help is appreciated
    • Jahangir Naik
      Hi, I haven’t worked much on the images using python .docx yet, maybe Document.style will be used(but not sure). If you have some queries still you can raise a ticket at support@webkul.com and we will assist you there.
      • Divyansh
        No, it’s alright I figured a workaround and besides, there’s no way to add z-index to an image in python docx right now.
        Thanks for your efforts, anyway.
        • Ankit
          Is there a way to change font name in pdf. As you have used a way of font size then might there be a way to change font name.
          • Jahangir
            Hi Ankit,
            You can change the in xls as shown below. May be this will help.

            style = document.styles[‘Normal’]
            font = style.font
            font.name = ‘Arial’
            font.size = Pt(10)

      • Megha Joshi
        Hello Divyansh,

        Thanks for the confirmation, for any further assistance kindly drop us a mail at – support@webkul.com

        Regards,
        Megha
        Webkul

  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again
    Close