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:
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[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')
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:
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 š
LikeLiked by 1 person
Awwww. You just made my day. Thanks Richitaroy š
LikeLike
I need to replace table cell text as color text it is possible.
LikeLike
Hi,
Lets say you have the case like this:
document:
*Table1:
*Table2: content
How to get the content of the table inside another table?
LikeLike
Sorry, it removed my spaces so added dots:
document:
……………..*Table1:
…………………………*Table2: content
LikeLike
hi, i have a requirement to access content of a table inside another table. any idea?
d
b
c
aa ad
ab af
ac ag
LikeLike
man, just what I was looking for. You’re a lifesaver
LikeLike
Thank you
LikeLiked by 1 person
Can i get the code for split the cell 5,0 into 5,1 in the same column
LikeLike
Can i get the code for split the cell 5,0 into 5,1 in the same cell and column
LikeLike
Great reading yourr blog post
LikeLike