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? – steeldriver Mar 22 '15 at 20:14
  • 1
    yes, I mean Microsoft Word document, version doesnt really matter, lets say the newest one. – user3294564 Mar 22 '15 at 22:46

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.