Working with Tables - Python .docx Module
Last Updated :
03 Jan, 2021
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.
Python docx module allows user to manipulate docs by either manipulating the existing one or creating a new empty document and manipulating it. It is a powerful tool as it helps you to manipulate the document to a very large extend. You can also add and manipulate tables using this module.
To add a table we will use add_table() method as it will add a table in the word document.
Syntax:
doc.add_table(rows = None, cols = None)
Parameters:
- rows: Add n number of rows in the table.
- cols: Add n number of cols in the table.
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 add the data in it.
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
Pip command to install this module is:
pip install python-docx
Approach
- Import module
- Declare docx object
- Add table data as a list
- Create table using above function
- Save to document
Example 1: Adding a table in a Word document.
Python3
# Import docx NOT python-docx
import docx
# Create an instance of a word document
doc = docx.Document()
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
# Table data in a form of list
data = (
(1, 'Geek 1'),
(2, 'Geek 2'),
(3, 'Geek 3')
)
# Creating a table object
table = doc.add_table(rows=1, cols=2)
# Adding heading in the 1st row of the table
row = table.rows[0].cells
row[0].text = 'Id'
row[1].text = 'Name'
# Adding data from the list to the table
for id, name in data:
# Adding a row and then adding data in it.
row = table.add_row().cells
# Converting id to string as table can only take string input
row[0].text = str(id)
row[1].text = name
# Now save the document to a location
doc.save('gfg.docx')
Output:
The table so obtained is a simple table, but docx supports mechanism to style it. To style a table we use style method to select a style.
Syntax:
table.style = String style_name
Parameter:
- String style_name: It is the name of the style from the list mentioned below.
Approach
- Import module
- Create data to be inserted as list
- Create table
- Style it as required
- Save to document
Example 2: Adding a table with style in a word document.
Python3
# Import docx NOT python-docx
import docx
# Create an instance of a word document
doc = docx.Document()
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
# Table data in a form of list
data = (
(1, 'Geek 1'),
(2, 'Geek 2'),
(3, 'Geek 3')
)
# Creating a table object
table = doc.add_table(rows=1, cols=2)
# Adding heading in the 1st row of the table
row = table.rows[0].cells
row[0].text = 'Id'
row[1].text = 'Name'
# Adding data from the list to the table
for id, name in data:
# Adding a row and then adding data in it.
row = table.add_row().cells
row[0].text = str(id)
row[1].text = name
# Adding style to a table
table.style = 'Colorful List'
# Now save the document to a location
doc.save('gfg.docx')
Output:
Similar Reads
Working with Lists - Python .docx Module Prerequisite: Working with .docx module 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 manipul
4 min read
Working with Titles and Heading - Python docx Module Prerequisites: docx 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 docume
2 min read
Working with Images - Python .docx Module 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
2 min read
Working with Page Break - Python .docx Module 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
2 min read
Working with Paragraphs in Python .docx Module Prerequisites: docx 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 docume
4 min read
Working with XlsxWriter Module - Python XlsxWriter is a Python module that provides various methods to work with Excel using Python. It can be used to read, write, applying formulas. Also, it supports features such as formatting, images, charts, page setup, auto filters, conditional formatting and many others.This tutorial aims at providi
7 min read
Working with Headers And Footers in Python .docx Module Prerequisite: Working with .docx module Word documents contain formatted text wrapped within three object levels. The Lowest level-run objects, middle level-paragraph objects, and highest level-document objects. So, we cannot work with these documents using normal text editors. But, we can manipulat
6 min read
Working with Excel Spreadsheets in Python You all must have worked with Excel at some time in your life and must have felt the need to automate some repetitive or tedious task. Don't worry in this tutorial we are going to learn about how to work with Excel using Python, or automating Excel using Python. We will be covering this with the hel
15 min read
Python MariaDB - Drop Table using PyMySQL MariaDB is an open source Database Management System and its predecessor to MySQL. The pymysql client can be used to interact with MariaDB similar to that of MySQL using Python. In this article we will look into the process of Dropping a table from a database using pymysql. To drop a table use any o
2 min read
How to make a Table in Python? Creating a table in Python involves structuring data into rows and columns for clear representation. Tables can be displayed in various formats, including plain text, grids or structured layouts. Python provides multiple ways to generate tables, depending on the complexity and data size.Using Tabula
3 min read