Python: How to Deal with Tables in Word

Header

I think I’ve mentioned it before that the Python documentation can be really confusing for beginners. There may be people out there who can look at this Table objects page and completely understand what’s happening.

I am not those people. I need visuals.

The Syntax

Before we get started, some housekeeping. We need to get the document we’re editing into a variable to make the script readable. Just pop it into something called doc for now.

doc = docx.Document('Filepath to the Document')

Once that’s done we can reference the tables by index number. Keep in mind, Python starts counting at zero, so to call up the first table in your document and print out whatever’s in the first cell, you’ll have to write something like this:

print(doc.tables[0].cell(0,0).text)

Immediately after seeing this, I thought to myself–what happens if you split a cell in a table? How does it keep count?

Python recognizes that split cell as it’s own new column or row and shifts everything over. Like so:

Table Cells

You can retrieve the contents of a cell from the Python shell by using the print function, but I was more concerned with how to edit the text in the cells. That looks like this:

TextReplace

A Completely Impractical Example for Reference

Here’s a thing you will never ever have to do. But it shows how a while loop could be plugged in for table automation. Fun, right?

]import docx
doc = docx.Document('C:\\Users\\Desktop\\TestCopy.docx')

num = 1
while num <= 10:
doc.tables[0].cell(num,1).text = str(num)
doc.tables[0].cell(num,0).text = 'This is another cell'
num += 1

doc.save('C:\\Users\\Desktop\\TestCopy.docx')

Test

How to Iterate through Every Cell in a Table
(A Slightly More Practical Example)

A slightly more useful thing to be able to do is going through ever cell in an existing table.

for row in doc.tables[0].rows:
for cell in row.cells:
use the cell variable to run the script on every cell

This has loads of applications—you could use regular expressions to find specific text within a table and adjust or remove them entirely. Or you could import the random module and get yourself a snazzy table with random numbers auto-populated. A five line example:

import docx

doc = docx.Document('C:\\Users\\Desktop\\TestCopy.docx')

for row in doc.tables[0].rows:
for cell in row.cells:
doc.save('C:\\Users\\Desktop\\TestCopy.docx')

That generates the table I’ve got at the top:

RandomizedTable

11 Replies to “Python: How to Deal with Tables in Word”

  1. Ah, its just like matrix!!! But you make readers understand the whole concept soo beautifully without getting into all that matrix stuff, and eventually make them learn the concepts of matrix, AWESOME(i hope i read this blog before my exams for maths and comp šŸ™‚ ). Your blog is really good šŸ™‚ Great post !! Have an awesome day šŸ™‚

    Liked by 1 person

  2. Hi,
    Lets say you have the case like this:
    document:
    *Table1:
    *Table2: content
    How to get the content of the table inside another table?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: