Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active April 8, 2022 18:55
Show Gist options
  • Save aspose-com-kb/957968cc38762e1261f7748d35016d34 to your computer and use it in GitHub Desktop.
Save aspose-com-kb/957968cc38762e1261f7748d35016d34 to your computer and use it in GitHub Desktop.
How to Edit DOCX in Python. For more information, please follow link: https://kb.aspose.com/words/python/how-to-edit-word-document-using-python/
import aspose.words as aw
# Initialize the license to avoid trial version watermark in the modified word file
editWordLicense = aw.License()
editWordLicense.set_license("Aspose.Word.lic")
# Load the source document that needs to be modified
docToEdit = aw.Document("input.docx")
# Instantiate the DocumentBuilder class object using the loaded document
builderToEdit = aw.DocumentBuilder(docToEdit)
# Move to the start of the document using the builder
builderToEdit.move_to_document_start()
# Set the font configurations for writing text
fontNewText = builderToEdit.font
fontNewText.size = 12
fontNewText.name = "Calibri"
fontNewText.underline = aw.Underline.SINGLE
# Set the paragraph formatting
paragraphFormat = builderToEdit.paragraph_format
paragraphFormat.first_line_indent = 6
paragraphFormat.alignment = aw.ParagraphAlignment.JUSTIFY
paragraphFormat.keep_together = True
# Write the text in the beginning of the document
builderToEdit.writeln("Here is a new paragraph added at the start of the document.")
docToEdit.save("modified.docx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment