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
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
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 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)
document.add_paragraph(sys.argv[3])
document.add_picture(sys.argv[4],width=Inches(6.0))
document.add_page_break()
document.save(sys.argv[1])
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
Reference:
I just modified its example, see its official documentation.
Notes:
