Ilovepdf Merged
Ilovepdf Merged
Search...
Word documents contain formatted text wrapped within three object levels.
The Lowest level-run objects, middle level-paragraph objects, and highest
level-document object. So, we cannot work with these documents using
normal text editors. But, we can manipulate these word documents in
python using the python-docx module. Pip command to install this module
is:
Font Size
To increase/decrease the font size of the text you have to first create a
paragraph object then you have to use add_run() method to add content.
You can directly use add_paragraph() method to add paragraph but if you
want to increase/decrease the font size of the text you have to use add_run()
as all the block-level formatting is done by using add_paragraph() method
while all the character-level formatting is done by using add_run().
Now to set a new font size we will use .font.size method. This is the
method of the font object and is used to set the new font size of the text.
Parameter:
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 1/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Python3
Output:
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 2/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Font Colour
To apply a font colour to the text you have to first create a paragraph object
then you have to use add_run() method to add content. You can directly use
add_paragraph() method to add paragraph but if you want to apply a font
colour to a text you have to use add_run() as all the block-level formatting is
done by using add_paragraph() method while all the character-level
formatting is done by using add_run().
To set the colour to the font we will make us of the RGBColor() object
which takes hexadecimal input of the colour and sets the same colour to the
text.
Parameter:
RGB Colour Value: It is the hexadecimal value of the colour you want
to set. It is given in the form of R, G, B as input.
Python3
import docx
from docx.shared import RGBColor
# Adding paragraph
doc.add_heading('Font Colour:', 3)
para = doc.add_paragraph().add_run(
'GeeksforGeeks is a Computer Science portal for geeks.')
Output:
Font Style
To set a new font style for the text you have to first create a paragraph
object then you have to use add_run() method to add content. You can
directly use add_paragraph() method to add paragraph but if you want to
set a new font style of the text you have to use add_run() as all the block-
level formatting is done by using add_paragraph() method while all the
character-level formatting is done by using add_run().
Now to set a new font name we will use .font.name method. This is the
method of the font object and is used to set the new font name for the text.
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 4/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Parameter:
String s: It is the name of the new font style. If you give any random
name as input string then default style is adopted for the text.
Python3
Output:
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 5/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Bold Text
doc.bold = True
To highlight a specific word the bold needs to be set True along with its
add_run() statement.
Python3
# Creating paragraph
para = doc.add_paragraph()
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 6/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Output:
Document gfg.docx
Python3
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 7/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Output:
Document gfg.docx
Italics Text
To set the text to italics you have to set it true.
doc.italic = True
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 8/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
To make some specific word(s) italics, it needs to be set True along with its
add_run() statement.
Python Course Python Tutorial Interview Questions Python Quiz Python Sign In
Python3
# Creating paragraph
para = doc.add_paragraph()
Output:
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 9/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Document gfg.docx
Python3
Output:
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 10/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Document gfg.docx
Underlined Text
To apply to underline to a text you have to set it true.
doc.underline = True
To underline a specific part, underline needs to set a True along with its
add_run() function
add_run("text").underline=True
Python3
# Creating paragraph
para = doc.add_paragraph()
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 11/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Output:
Document gfg.docx
Python3
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 12/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Output:
Document gfg.docx
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 13/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
Similar Reads
Creating Ebooks with borb in Python
Creating ebooks can be a rewarding endeavor, whether for sharing knowledge,
storytelling, or distributing documentation. The borb library in Python is a…
13 min read
2 min read
2 min read
2 min read
2 min read
2 min read
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 14/18
4/22/25, 6:06 AM Working With Text In Python .docx Module | GeeksforGeeks
3 min read
2 min read
2 min read
6 min read
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida,
Gautam Buddh Nagar, Uttar
Pradesh, 201305
https://www.geeksforgeeks.org/working-with-text-in-python-docx-module/ 15/18
4/22/25, 6:08 AM Working with Tables – Python .docx Module | GeeksforGeeks
Search...
Python Course Python Tutorial Interview Questions Python Quiz Python Glossary Pytho
Prerequisites: docx
Word documents contain formatted text wrapped within three object levels.
Lowest level- run objects, middle level- paragraph objects and highest
level- document object. So, we cannot work with these documents using
normal text editors. But, we can manipulate these word documents in
python using the python-docx module.
To add a table we will use add_table() method as it will add a table in the
word document.
Syntax:
Parameters:
First, we will save all the data in a list then we will create a table object
with values of rows = 1 and cols = 2. Then we will add the headings in the
table. After that, we will use .add_row() method to add a row then we will
We add
use cookies to ensure
the data in it. you have the best browsing experience on our
website. By using our site, you acknowledge that you have read and Got It !
understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/working-with-tables-python-docx-module/ 1/9
4/22/25, 6:08 AM Working with Tables – Python .docx Module | GeeksforGeeks
Table can only take a string as an input in its cells, so we have to convert
the data into string if it is not.
Installation
Approach
Import module
Declare docx object
Add table data as a list
Create table using above function
Save to document
Python3
Output:
Syntax:
Parameter:
We use cookies to ensure you have the best browsing experience on our
website. By using our site, you acknowledge that you have read and
Approach
understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/working-with-tables-python-docx-module/ 3/9
4/22/25, 6:08 AM Working with Tables – Python .docx Module | GeeksforGeeks
Import module
Create data to be inserted as list
Create table
Style it as required
Save to document
Python3
Output:
Similar Reads
How to read specific lines from a File in Python?
Text files are composed of plain text content. Text files are also known as flat
files or plain files. Python provides easy support to read and access the…
10 min read
4 min read
2 min read
2 min read
2 min read
2 min read
2 min read
3 min read
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida,
Gautam Buddh Nagar, Uttar
Pradesh, 201305
Advertise with us
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal GfG Weekly Contest
Privacy Policy Offline Classroom Program
Careers DSA in JAVA/C++
In Media Master System Design
Contact Us Master CP
GfG Corporate Solution GeeksforGeeks Videos
Placement Training Program
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
We use cookies to ensure you have the best browsing experience on our
GoLang DSA Roadmap
website. By using our site, you acknowledge that you have read and
understoodSQL
our Cookie Policy & Privacy Policy DSA Interview Questions
https://www.geeksforgeeks.org/working-with-tables-python-docx-module/ 7/9
4/22/25, 6:08 AM Working with Tables – Python .docx Module | GeeksforGeeks
Quickstart
Getting started with python-docx is easy. Let’s walk through the basics.
Opening a document
First thing you’ll need is a document to work on. The easiest way is this:
document = Document()
Adding a paragraph
Paragraphs are fundamental in Word. They’re used for body text, but also
for headings and list items like bullets.
https://python-docx.readthedocs.io/en/latest/user/quickstart.html 1/9
4/22/25, 6:04 AM Quickstart — python-docx 1.1.2 documentation
It’s also possible to use one paragraph as a “cursor” and insert a new para-
graph directly above it:
prior_paragraph = paragraph.insert_paragraph_before('Lo
Adding a heading
In anything but the shortest document, body text is divided into sections,
each of which starts with a heading. Here’s how to add one:
https://python-docx.readthedocs.io/en/latest/user/quickstart.html 2/9
4/22/25, 6:04 AM Quickstart — python-docx 1.1.2 documentation
If you find yourself using this very often, it’s probably a sign you could ben-
efit by better understanding paragraph styles. One paragraph style property
you can set is to break a page immediately before each paragraph having
that style. So you might set your headings of a certain level to always start
a new page. More on styles later. They turn out to be critically important for
really getting the most out of Word.
Adding a table
One frequently encounters content that lends itself to tabular presentation,
lined up in neat rows and columns. Word does a pretty good job at this.
Here’s how to add a table:
Tables have several properties and methods you’ll need in order to popu-
late them. Accessing individual cells is probably a good place to start. As a
baseline, you can always access a cell by its row and column indicies:
cell = table.cell(0, 1)
This gives you the right-hand cell in the top row of the table we just cre-
ated. Note that row and column indicies are zero-based, just like in list
access.
Frequently it’s easier to access a row of cells at a time, for example when
populating a table of variable length from a data source. The .rows prop-
erty of a table provides access to individual rows, each of which has a
.cells property. The .cells property on both Row and Column supports in-
latest
dexed access, like a list:
https://python-docx.readthedocs.io/en/latest/user/quickstart.html 3/9
4/22/25, 6:04 AM Quickstart — python-docx 1.1.2 documentation
row = table.rows[1]
row.cells[0].text = 'Foo bar to you.'
row.cells[1].text = 'And a hearty foo bar to you too s
The .rows and .columns collections on a table are iterable, so you can use
them directly in a for loop. Same with the .cells sequences on a row or
column:
If you want a count of the rows or columns in the table, just use len() on
the sequence:
row_count = len(table.rows)
col_count = len(table.columns)
row = table.add_row()
This can be very handy for the variable length table scenario we mentioned
above:
https://python-docx.readthedocs.io/en/latest/user/quickstart.html 4/9
4/22/25, 6:04 AM Quickstart — python-docx 1.1.2 documentation
heading_cells[1].text = 'SKU'
heading_cells[2].text = 'Description'
The same works for columns, although I’ve yet to see a use case for it.
Word has a set of pre-formatted table styles you can pick from its table
style gallery. You can apply one of those to the table like this:
table.style = 'LightShading-Accent1'
The style name is formed by removing all the spaces from the table style
name. You can find the table style name by hovering your mouse over its
thumbnail in Word’s table style gallery.
Adding a picture
Word lets you place an image in a document using the Insert > Photo >
Picture from file... menu item. Here’s how to do it in python-docx:
document.add_picture('image-filename.png')
This example uses a path, which loads the image file from the local filesys-
tem. You can also use a file-like object, essentially any object that acts like
an open file. This might be handy if you’re retrieving your image from a
database or over a network and don’t want to get the filesystem involved.
Image size
By default, the added image appears at native size. This is oftenlatest
bigger
than you want. Native size is calculated as pixels / dpi. So a 300x300
https://python-docx.readthedocs.io/en/latest/user/quickstart.html 5/9
4/22/25, 6:04 AM Quickstart — python-docx 1.1.2 documentation
pixel image having 300 dpi resolution appears in a one inch square. The
problem is most images don’t contain a dpi property and it defaults to 72
dpi. This would make the same image appear 4.167 inches on a side,
somewhere around half the page.
To get the image the size you want, you can specify either its width or
height in convenient units, like inches or centimeters:
document.add_picture('image-filename.png', width=Inche
You’re free to specify both width and height, but usually you wouldn’t want
to. If you specify only one, python-docx uses it to calculate the properly
scaled value of the other. This way the aspect ratio is preserved and your
picture doesn’t look stretched.
The Inches and Cm classes are provided to let you specify measurements
in handy units. Internally, python-docx uses English Metric Units, 914400
to the inch. So if you forget and just put something like width=2 you’ll get
an extremely small image :). You’ll need to import them from the
docx.shared sub-package. You can use them in arithmetic just like they
were an integer, which in fact they are. So an expression like width =
Inches(3) / thing_count works just fine.
You can apply a paragraph style right when you create a paragraph:
latest
https://python-docx.readthedocs.io/en/latest/user/quickstart.html 6/9
4/22/25, 6:04 AM Quickstart — python-docx 1.1.2 documentation
The style is specified using its style name, ‘List Bullet’ in this example.
Generally, the style name is exactly as it appears in the Word user interface
(UI).
https://python-docx.readthedocs.io/en/latest/user/quickstart.html 7/9
4/22/25, 6:04 AM Quickstart — python-docx 1.1.2 documentation
This produces a paragraph that looks just like one created from a single
string. It’s not apparent where paragraph text is broken into runs unless you
look at the XML. Note the trailing space at the end of the first string. You
need to be explicit about where spaces appear at the beginning and end of
a run. They’re not automatically inserted between runs. Expect to be
caught by that one a few times :).
Run objects have both a .bold and .italic property that allows you to set
their value for a run:
which produces text that looks like this: ‘Lorem ipsum dolor sit amet.’
Note that you can set bold or italic right on the result of .add_run() if you
don’t need it for anything else:
paragraph.add_run('dolor').bold = True
# is equivalent to:
run = paragraph.add_run('dolor')
run.bold = True
paragraph = document.add_paragraph()
paragraph.add_run('Lorem ipsum ')
paragraph.add_run('dolor').bold = True latest
paragraph.add_run(' sit amet.')
https://python-docx.readthedocs.io/en/latest/user/quickstart.html 8/9
4/22/25, 6:04 AM Quickstart — python-docx 1.1.2 documentation
Like paragraph styles, a character style must already be defined in the doc-
ument you open with the Document() call (see Understanding Styles).
You can also apply a style to a run after it is created. This code produces
the same result as the lines above:
As with a paragraph style, the style name is as it appears in the Word UI.
AI-powered ad network for devs. Get your message in front of the right
developers with EthicalAds.
Ads by EthicalAds
latest
https://python-docx.readthedocs.io/en/latest/user/quickstart.html 9/9