The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Word Document Formatting via Python

Word document formatting using Python is a powerful way to automate the creation and styling of Word files, making workflows more efficient and consistent. The most popular library for this task is python-docx, which allows you to create, modify, and style Microsoft Word documents (.docx) programmatically.

Installing python-docx

To get started, install the library using pip:

bash
pip install python-docx

Creating a Word Document

You can create a new document and add paragraphs easily:

python
from docx import Document doc = Document() doc.add_paragraph("This is the first paragraph.") doc.save("example.docx")

Adding and Formatting Text

You can control font style, size, boldness, italics, and underline within paragraphs:

python
from docx.shared import Pt doc = Document() p = doc.add_paragraph() run = p.add_run("This is bold text, ") run.bold = True run = p.add_run("and this is italic text.") run.italic = True p.style = 'Normal' doc.save("formatted.docx")

Fonts and Sizes

You can specify fonts and sizes using run.font:

python
from docx.shared import Pt from docx.oxml.ns import qn p = doc.add_paragraph() run = p.add_run("Custom font and size") font = run.font font.name = 'Arial' font.size = Pt(14) # For East Asian fonts, use qn for correct XML tagging font.element.rPr.rFonts.set(qn('w:eastAsia'), 'Arial') doc.save("custom_font.docx")

Paragraph Alignment and Spacing

Paragraph alignment can be set to left, right, center, or justify:

python
from docx.enum.text import WD_ALIGN_PARAGRAPH p = doc.add_paragraph("Centered paragraph") p.alignment = WD_ALIGN_PARAGRAPH.CENTER doc.save("alignment.docx")

Adjust line spacing and spacing before/after paragraphs:

python
from docx.shared import Pt p_format = p.paragraph_format p_format.line_spacing = 1.5 p_format.space_before = Pt(12) p_format.space_after = Pt(12) doc.save("spacing.docx")

Adding Headings

Use predefined heading styles for better document structure:

python
doc.add_heading('Heading Level 1', level=1) doc.add_heading('Heading Level 2', level=2) doc.save("headings.docx")

Working with Lists

Create ordered or unordered lists:

python
doc.add_paragraph('First item in bullet list', style='List Bullet') doc.add_paragraph('Second item in bullet list', style='List Bullet') doc.add_paragraph('First item in numbered list', style='List Number') doc.add_paragraph('Second item in numbered list', style='List Number') doc.save("lists.docx")

Adding Tables

Tables can be created and styled as well:

python
table = doc.add_table(rows=3, cols=3) table.style = 'LightShading-Accent1' for row in table.rows: for cell in row.cells: cell.text = "Cell" doc.save("table.docx")

Adding Images

You can insert images into the document, specifying width or height:

python
doc.add_picture('image.png', width=Pt(300)) doc.save("image.docx")

Advanced Formatting: Styles and Themes

You can modify existing styles or create custom styles by accessing the styles object:

python
styles = doc.styles normal_style = styles['Normal'] font = normal_style.font font.name = 'Times New Roman' font.size = Pt(12) doc.save("styled.docx")

Automating Bulk Document Creation

By combining loops and conditional logic, you can generate reports, invoices, or any documents with complex formatting automatically.


Summary:
Python’s python-docx library is an essential tool for automated Word document creation and formatting. It provides comprehensive features to manipulate text, paragraphs, styles, lists, tables, and images with ease. Whether you need simple text edits or fully formatted reports, Python scripting streamlines the process significantly.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About