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
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
You may use
python-docx to create MS Word document by creating an intermediate script to handle the shell arguments.
Install required libraries:
sudo apt-get install python-pip libxslt1-dev python-lxml sudo pip install python-docx
Write the intermediate script and let's name it
#!/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, 0) document.add_paragraph(sys.argv) document.add_picture(sys.argv,width=Inches(6.0)) document.add_page_break() document.save(sys.argv) sys.exit(0)
Give it the right permissions
chmod +x docx-gen
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.