Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save documentprocessing/cf556694ddfc86f175439ca46183dbe4 to your computer and use it in GitHub Desktop.
Save documentprocessing/cf556694ddfc86f175439ca46183dbe4 to your computer and use it in GitHub Desktop.
Edit Word Documents (.docx) using python-docx library. Check [URL] for more details.
# Import necessary modules from the python-docx library
from docx import Document
from docx.shared import Pt
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
# Create a new Document object
doc = Document()
# Add a title to the document
title = doc.add_heading('Table Handling Example', level=1)
# Add a table with 3 rows and 4 columns to the document
table = doc.add_table(rows=3, cols=4)
# Apply a table style ('Table Grid') to the table
table.style = 'Table Grid'
# Loop through each row in the table
for row in table.rows:
# loop through each cell in the row
for cell in row.cells:
# Set the cell text to 'Cell Content'
cell.text = 'Cell Content'
# Loop through each row and cell in the table to format the cell content
for row in table.rows:
for cell in row.cells:
# Set vertical alignment of the cell content to center
cell.paragraphs[0].alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
# Set the font size of the cell content to 12 points
cell.paragraphs[0].runs[0].font.size = Pt(12)
# Make the cell content bold
cell.paragraphs[0].runs[0].bold = True
# Save the document with the specified file name
doc.save('table_handling_example.docx')
# Print a success message to indicate that the table has been added and formatted successfully
print("Table added and formatted successfully.")
# Import necessary modules from the python-docx library
from docx import Document
from docx.shared import Inches
# Open an existing Word document
doc = Document("table_handling_example.docx")
# Add a title for the image section
image_title = doc.add_heading('Image Example', level=1)
# Center align the title
image_title.alignment = 1
# Add a paragraph with a description
image_description = doc.add_paragraph('Here is an image added to the document:')
# Replace with the path to your image file
image_path = '0Image9.png'
# Add the image to the document
doc.add_picture(image_path, width=Inches(3), height=Inches(2))
# Save the modified document
doc.save('table_handling_example.docx')
# Import necessary modules from the python-docx library
from docx import Document
from docx.shared import Pt
# Open an existing Word document
doc = Document("documentprocessing.docx")
# Access the first paragraph in the document
first_paragraph = doc.paragraphs[0]
# Apply formatting to all runs within the paragraph
for run in first_paragraph.runs:
# Set the font size to 14 points
run.font.size = Pt(14)
# Make the text bold
run.font.bold = True
# Italicize the text
run.font.italic = True
# Underline the text
run.font.underline = True
# Save the modified document to a new file
doc.save("formatted_document.docx")
# Import the Document class from the python-docx library
from docx import Document
# Open an existing Word document
doc = Document("documentprocessing.docx")
# Access the first paragraph of the document
first_paragraph = doc.paragraphs[0]
# Print the original text of the first paragraph
print("Original Text:\n", first_paragraph.text)
# Define new text to replace the original text in the first paragraph
new_text = "This is the modified text in the first paragraph."
# Clear the existing content of the first paragraph
first_paragraph.clear()
# Add the new text to the first paragraph
first_paragraph.add_run(new_text)
# Print the modified text of the first paragraph
print("\nModified Text:\n", first_paragraph.text)
# Save the modified document with a new filename
doc.save("modified_document.docx")
# Print a success message indicating that the document has been edited and saved
print("\nDocument edited and saved successfully.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment