0

I am a newbie and I started learning Python on my own by seeing videos. I have a task to read table from word document using python and populate it to database.

I can able to write the code to read the paragraphs by using the below code. Can anyone please guide me how to write the code for reading the table form word document? Thanks

import docx
doc = docx.Document('Text.docx')
doc.paragraphs
doc.paragraphs[0].text
doc.paragraphs[1].text

Samlpe table:

Heading Name1 Desc1 Desc1 Desc2 Name3 Desc3 Name4 Desc4 Desc1 Desc5 Name6 Desc6 Name7 Desc7

I tried writing code to read paragraphs but I am searching how to write the code to read the table

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Seetha
  • 1
  • 1

1 Answers1

0

You can use the docx library:

from docx import Document

doc = Document('Text.docx')

for table in doc.tables:
    for row in table.rows:
        for cell in row.cells:
            print cell.text

Similar question can be found here: How to read contents of an Table in MS-Word file Using Python?