I am trying to create a file.doc (Microsoft Word Document format) and write some text into it and also, insert an image into it via command shell. I have no idea what or how should I do it.

Any suggestions? Thanks

  • 1
    First, you need to define what you mean by "a file.doc" - do you mean a file that is compatible with Microsoft's Word document (.doc) format? if so, which version? Mar 22, 2015 at 20:14
  • 1
    yes, I mean Microsoft Word document, version doesnt really matter, lets say the newest one. Mar 22, 2015 at 22:46

1 Answer 1


You may use python-docx to create MS Word document by creating an intermediate script to handle the shell arguments.

  1. Install required libraries:

    sudo apt-get install python-pip libxslt1-dev python-lxml
    sudo pip install python-docx
  2. Write the intermediate script and let's name it docx-gen

    #!/usr/bin/env python
    import sys
    from docx import Document
    from docx.shared import Inches
    if __name__=="__main__":
        document = Document()
        document.add_heading(sys.argv[2], 0)
  3. Give it the right permissions

    chmod +x docx-gen
  4. Use it locally

    ./docx-gen demo.docx "Title" "A paragraph with few words." ~/Pictures/snapshot1.png

    or copy it to the system bin folder:

    sudo cp docx-gen /usr/local/bin/

    then to use it:

    docx-gen demo.docx "Title" "A paragraph with few words." ~/Pictures/snapshot1.png


I just modified its example, see its official documentation.


  • The script does not include any input check, just to keep it simple.
  • Used fixed width of 6 inches, you may add it to input arguments too.

demo document for python-docx

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .