Skip to main content
Practice

Automating DOCX Document Creation

For simple documents, it is often quicker to create them directly in a word processing program.

But what about these situations?

  • When you need to insert data in table form from an Excel file that includes a large amount of data into a Word file

  • When you have recurring tasks, such as monthly sales reports, using the same document template repeatedly

In such cases, creating documents with a Python program to handle repetitive tasks efficiently is much more effective.

Click the ▶︎ Run button marked in green in the code editor to see the table generated in the Word file!


Easy-to-Read Code Explanation

There's no need to feel overwhelmed by what seems like complex code.

By learning Python basics and writing code with the help of AI, you will be able to fully understand how the code in the practice screen works.

Leave the coding to AI. You just need to understand the principles.


1. Importing Required Libraries

Importing openpyxl and python-docx Libraries
# Library for handling Excel files
import openpyxl

# Library for creating and editing Word documents
from docx import Document

2. Loading the Excel File

Loading an Excel File with load_workbook
# Load the Excel file containing data
wb = openpyxl.load_workbook('input_file.xlsx')

# Get the currently active sheet
sheet = wb.active

3. Creating a Word Document

Creating a Word Document with the Document Class
# Create a new Word document
doc = Document()

4. Adding a Report Title

Adding a Title with the add_heading Function
# Add a title to the Word document
doc.add_heading('2024 Q1 Sales Performance Report', 0)

5. Creating a Table and Setting Column Headers

Creating a Table and Setting Column Headers
# Create a table with 1 row and 5 columns
table = doc.add_table(rows=1, cols=5)

# Add a border style to the table
table.style = 'Table Grid'

# Set the first row (column headers)
hdr_cells = table.rows[0].cells

# Set column headers
hdr_cells[0].text = 'Month'
hdr_cells[1].text = 'Product'
hdr_cells[2].text = 'Quantity Sold'
hdr_cells[3].text = 'Unit Price'
hdr_cells[4].text = 'Revenue'

6. Adding Excel Data and Saving the Word Document

Adding Excel Data and Saving the Word Document
# Read data from the second row and add it to the table
for row in sheet.iter_rows(min_row=2, values_only=True):
# Save each column's data into variables
month, product, sales, price, revenue = row

# Add a new row to the table
row_cells = table.add_row().cells
row_cells[0].text = str(month) # Convert 'Month' data to text and add it
row_cells[1].text = product # Add 'Product' data
row_cells[2].text = str(sales) # Convert 'Quantity Sold' data to text and add it
row_cells[3].text = str(price) # Convert 'Unit Price' data to text and add it
row_cells[4].text = str(revenue) # Convert 'Revenue' data to text and add it

# Save the Word document as 'output_file.docx'
doc.save('output_file.docx')

In the upcoming course, we will cover in detail how to create .xlsx, .pptx, and .docx files using Python code in Essential Knowledge for Automating Work Tasks Chapters 1 and 2.

For now, enjoy running the code and getting a preview of what you'll learn! 🚀