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.
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:
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:
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:
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.cell(num,1).text = str(num) doc.tables.cell(num,0).text = 'This is another cell' num += 1 doc.save('C:\\Users\\Desktop\\TestCopy.docx')
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.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.rows: for cell in row.cells: doc.save('C:\\Users\\Desktop\\TestCopy.docx')
That generates the table I’ve got at the top: