0

I am looking to iterate through sentences/paragraphs within cells of a docx table, performing functions depending on their style tags using the pywin32 module.

I can manually select the cell using cell = table.Cell(Row = 1, Column =2)

I tried using something like

for x in cell:
#do something

but <class 'win32com.client.CDispatch'> objects 'do not support enumeration'

I tried looking through: Word OM to find a solution but to no avail (I understand this is for VBA, but still can be very useful)

Izaak van Dongen
  • 2,450
  • 13
  • 23
The Mob
  • 391
  • 3
  • 10
  • 1
    Does [this discussion](https://stackoverflow.com/questions/10366596/how-to-read-contents-of-an-table-in-ms-word-file-using-python) answer your question? – Gerd Jan 13 '20 at 12:11
  • Its works yes. The problem I am having is words are being printed character by character. How can I print them word by word? ```for word in table.Cell(Row =2, Column =2).Range.Text: print(word)``` – The Mob Jan 14 '20 at 00:19

1 Answers1

1

Here is a simple example that reads the content from the the first row / first column of the first table in a document and prints it word-by-word:

import win32com.client as win32
import os

wordApp = win32.gencache.EnsureDispatch("Word.Application")
wordApp.Visible = False
doc = wordApp.Documents.Open(os.getcwd() + "\\Test.docx")
table = doc.Tables(1)
for word in table.Cell(Row = 1, Column = 1).Range.Text.split():
    print(word)
wordApp.Application.Quit(-1)

The cell's content is just a string, you could easily also split it by paragraphs using split('\r') or by sentences using split('.').

Gerd
  • 2,568
  • 1
  • 7
  • 20
  • Thanks @Gerd. Is a manual iteration the only the way to cycle through the cells? e.g. ```(Row = counter, Column = counter1).Range.Text.split():``` – The Mob Jan 17 '20 at 03:38
  • @TheMob: Yes, this is what I would do. Number of rows and columns can be determined by `table.Rows.Count` and `table.Columns.Count`. – Gerd Jan 17 '20 at 07:47