Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active November 5, 2024 13:45
Show Gist options
  • Save aspose-com-gists/bcb11fd8e7050e31f052037012c38864 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/bcb11fd8e7050e31f052037012c38864 to your computer and use it in GitHub Desktop.
Python MS Word Automation – Create, Edit, or Convert MS Word Documents using Python

Learn how to automate MS Word to create, edit, or convert Word documents using Python: https://blog.aspose.com/2022/05/31/python-ms-word-automation-create-edit-or-convert-ms-word-documents-using-python/

This article covers all the basic features required for generating and manipulating Word documents programmatically using Python. Once you read this article, you will be able to:

  1. Python Word Automation API to Create, Edit, or Convert Word Documents
  2. Create Word Documents
  3. Edit or Modify Word Documents
  4. Find and Replace Text in Word Documents
  5. Convert Word Documents
  6. Parse Word Documents
import aspose.words as aw
# This code example demonstrates how to convert a Word document to PDF.
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample_output.docx")
# Specify save options
saveOptions = aw.saving.HtmlSaveOptions()
saveOptions.css_style_sheet_type = aw.saving.CssStyleSheetType.EXTERNAL
saveOptions.export_font_resources = True
saveOptions.resource_folder = "C:\\Files\\Resources"
saveOptions.resource_folder_alias = "C:/Files/resources"
# Save the converted document
doc.save("C:\\Files\\Converted.html", saveOptions)
import aspose.words as aw
# This code example demonstrates how to create a new Word document using Python.
# Create document object
doc = aw.Document()
# Create a document builder object
builder = aw.DocumentBuilder(doc)
# Specify font formatting Font
font = builder.font
font.size = 32
font.bold = True
font.name = "Arial"
font.underline = aw.Underline.SINGLE
# Insert text
builder.writeln("Welcome")
builder.writeln()
# Set paragraph formatting
font.size = 14
font.bold = False
font.name = "Arial"
font.underline = aw.Underline.NONE
paragraphFormat = builder.paragraph_format
paragraphFormat.first_line_indent = 8
paragraphFormat.alignment = aw.ParagraphAlignment.JUSTIFY
paragraphFormat.keep_together = True
# Insert paragraph
builder.writeln('''Aspose.Words for Python is a class library that enables your applications to perform a great range of document processing tasks.
It supports most of the popular document formats such as DOC, DOCX, RTF, HTML, Markdown, PDF, XPS, EPUB, and others.
With the API, you can generate, modify, convert, render, and print documents without third-party applications or Office Automation.
''')
builder.writeln()
# Insert a Table
font.bold = True
builder.writeln("This is a sample table")
font.bold = False
# Start table
table = builder.start_table()
# Insert cell
builder.insert_cell()
table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_CONTENTS)
# Set formatting and add text
builder.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER
builder.write("Row 1 cell 1")
builder.insert_cell()
builder.write("Row 1 cell 2")
builder.end_row()
builder.insert_cell()
builder.write("Row 2 cell 1")
builder.insert_cell()
builder.write("Row 2 cell 2")
builder.end_row()
# End table
builder.end_table()
builder.writeln()
# Insert image
builder.insert_image("C:\\Files\\aspose-icon.png")
# Save document
doc.save("C:\\Files\\sample_output.docx")
import aspose.words as aw
# This code example demonstrates how to edit an existing Word document.
# Load the document
doc = aw.Document("C:\\Files\\sample_output.docx")
# Initialize document builder
builder = aw.DocumentBuilder(doc)
# Access the paragraph
paragraph = doc.sections[0].body.paragraphs[0].runs[0]
paragraph.text = "This is an updated text!"
# Save the document
doc.save("C:\\Files\\sample_updated.docx")
import aspose.words as aw
# This code example demonstrates how to find and replace text in Word document.
# Load the document
doc = aw.Document("C:\\Files\\sample_output.docx")
# Update using find and replace
# Specify the search string and replace string using the Replace method.
doc.range.replace("Aspose.Words", "Hello",
aw.replacing.FindReplaceOptions(aw.replacing.FindReplaceDirection.FORWARD))
# Save the document
doc.save("C:\\Files\\find_and_replace.docx")
import aspose.words as aw
# This code example demonstrates how to parse a Word document.
# Load the document
doc = aw.Document("C:\\Files\\Sample.docx")
# Extract text
print(doc.range.text)
# Save as plain text
doc.save("C:\\Files\\output.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment