I'm reading a .docx file to extract the tables it has in it and re-write each table to a .txt file. I am able to print the rows and columns in the terminal and create all .txt files for each table, but the files created are empty. Here is what I have:
from docx import Document
document = Document('my_doc.docx')
tables = document.tables #Stores all tables in this variable
c = 1
for table in tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
print(paragraph.text)
with open('table_'+str(c)+'.txt', 'w') as f:
f.write(paragraph.text)
c += 1
If I do f.write(str(paragraph)) instead of f.write(paragraph.text), it only writes the location where the tables are stored. What am I doing wrong? How can I save the actual content of the tables to the text files?
Thanks!