In this article we want to talk about Working with Microsoft Word Documents in Python, for this lesson we want to use Python-Docx, so first of all let’s talk about Python-Docx library.
What is Python-Docx ?
Python-Docx is Python library that allows you to create, modify and extract content from Microsoft Word documents. Using Python-Docx you can automate the creation of professional quality reports, resumes, invoices and other types of documents in programmatic way.
Installing Python-Docx
So for Working with Microsoft Word Documents in Python, first you need to install that, and you can use pip for the installation of Python-Docx, open your terminal or command prompt and write this command.
1 |
pip install python-docx |
Create New Word Document with Python
For creating new Word document using Python-Docx, you can create an instance of Document class:
1 2 3 |
from docx import Document document = Document() |
This will create a new, empty Word document. You can now add content to the document by calling different methods of the Document class.
Also this code creates a new word document in your current directory.
1 2 3 4 5 6 7 |
from docx import Document # Create a new document document = Document() # Save the document with a specific name document.save("file.docx") |
Adding Content to Word Document with Python
Python-Docx allows you to add alot of of content to your Word documents, including paragraphs, headings, tables, images and many more. This is an example of how to add paragraph to a document:
1 2 3 4 5 6 7 8 9 10 |
from docx import Document # Open existing Word document existing_document = Document("file.docx") # Add paragraph to the existing document paragraph = existing_document.add_paragraph('This is a paragraph.') # Save changes to the existing document existing_document.save("file.docx") |
This will add new paragraph to the document with the text “This is a paragraph.”
You can also add headings to your document using the add_heading method:
1 2 3 4 5 6 7 8 9 10 |
from docx import Document # Create new Word document document = Document() # Add heading to the document heading = document.add_heading('This is a heading', level=1) # Save the document document.save("file.docx") |
Modifying Formatting of Word with Python
Python-Docx allows you to modify the formatting of your document’s content in different ways. For example you can change the font, size and color of text, and you can apply styles to paragraphs and headings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from docx import Document from docx.shared import Pt, RGBColor # Create new Word document document = Document() # Add paragraph to the document paragraph = document.add_paragraph('This is a paragraph.') # Set font name and size of the paragraph paragraph.style.font.name = 'Times New Roman' paragraph.style.font.size = Pt(12) # Set font color of the paragraph paragraph_font = paragraph.style.font paragraph_font.color.rgb = RGBColor(0, 0, 0) # Black color # Save the document document.save("file.docx") |
This will change the font of the paragraph to Times New Roman, set the font size to 12 points, and change the font color to black.
You can also apply styles to paragraphs and headings using the style property:
1 2 3 4 5 |
from docx import Document document = Document() paragraph = document.add_paragraph('This is a paragraph.') paragraph.style = 'Heading 1' |
FAQs:
How to work with Word docs in Python?
For working with Word documents in Python, you can use python-docx library. First you need to install the library using pip: pip install python-docx. After that you can create, read and modify Word documents programmatically using Document class provided by the python-docx library. You can add paragraphs, headings, tables, images and more to your Word documents using this library.
What is the best Python library for Word documents?
One of the most popular Python libraries for working with Word documents is python-docx. It provides different features for creating, reading and modifying Word documents. Using python-docx, you can handle different elements of Word documents, including paragraphs, headings, styles, tables, images and many more. It’s actively maintained and well-documented, and this is a popular choice for working with Word documents in Python.
Subscribe and Get Free Video Courses & Articles in your Email