Create/ Update docx file using Python
In this video it shows the Python code which can be used to programmatically create or update a doc/ docx (document) file in your Windows OS.
It imports document from docx library which is part of ‘python-docx’. So, if not already present then it can be installed using the below command:
$ pip show python-docx
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Details:
Python Code:
import os
from docx import Document
def create_update_doc(file_path, content):
if os.path.exists(file_path):
doc = Document(file_path)
else:
doc = Document()
doc.add_paragraph(content)
doc.save(file_path)
create_update_doc("C:\\Work\\Python_demo\\example1.docx", "This is new content for the document from Programmer World 3rd time.")
Screenshots: