this error was reported in the form in the header
@xsduan The file saved as '.docx'. If I convert the word to 2007/2010, it will not report an error.
According to the changes made by your code, I can directly deal with this problem and thank you very much. However, your code reported "NameError: name 'InvalidXmlError' is not defined". I changed 'InvalidXmlError' to 'Exception'. So, is there good error to replace Exception? Thank you!
@xsduan docx.oxml.exceptions.InvalidXmlError: required <w:tblGrid> child element not present

If you're having trouble extracting the data from a table, perhaps the following code can help you.
Fisrt is the quick monkey patch refer in https://github.com/python-openxml/python-docx/issues/547, It's part of the solution. If your table has a different number of cells per row, the problem persists.
You can refer this code:
data = []
try:
for row in b.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
data.append(paragraph.text)
except Exception as e:
for tc in b._tbl.iter_tcs():
cell = _Cell(tc, b)
for b_tc in iter_block_items(cell):
if isinstance(b_tc, Paragraph):
data.append(b_tc.text)
def iter_block_items(parent):
"""
Yield each paragraph and table child within *parent*, in document order.
Each returned value is an instance of either Table or Paragraph. *parent*
would most commonly be a reference to a main Document object, but
also works for a _Cell object, which itself can contain paragraphs and tables.
"""
if isinstance(parent, Document_c):
parent_elm = parent.element.body
elif isinstance(parent, _Cell):
parent_elm = parent._tc
else:
raise ValueError("something's not right")
for child in parent_elm.iterchildren():
if isinstance(child, CT_P):
yield Paragraph(child, parent)
elif isinstance(child, CT_Tbl):
yield Table(child, parent)
what is 'b' in that except block
from docx import Document
from docx.table import _Cell, Table
doc = Doument('your file')
for b in iter_block_doc:
if isinstance(b, Table):
......
If you're having trouble extracting the data from a table, perhaps the following code can help you.
Fisrt is the quick monkey patch refer in https://github.com/python-openxml/python-docx/issues/547, It's part of the solution. If your table has a different number of cells per row, the problem persists.
You can refer this code:data = [] try: for row in b.rows: for cell in row.cells: for paragraph in cell.paragraphs: data.append(paragraph.text) except Exception as e: for tc in b._tbl.iter_tcs(): cell = _Cell(tc, b) for b_tc in iter_block_items(cell): if isinstance(b_tc, Paragraph): data.append(b_tc.text) def iter_block_items(parent): """ Yield each paragraph and table child within *parent*, in document order. Each returned value is an instance of either Table or Paragraph. *parent* would most commonly be a reference to a main Document object, but also works for a _Cell object, which itself can contain paragraphs and tables. """ if isinstance(parent, Document_c): parent_elm = parent.element.body elif isinstance(parent, _Cell): parent_elm = parent._tc else: raise ValueError("something's not right") for child in parent_elm.iterchildren(): if isinstance(child, CT_P): yield Paragraph(child, parent) elif isinstance(child, CT_Tbl): yield Table(child, parent)
But what is the Document_c type? How can we import from the docx? Thank you.
you can save your file as Office Open XML Text (.docx), your current file format is Word 2007-365 (.docx)
Most helpful comment
@xsduan docx.oxml.exceptions.InvalidXmlError: required

<w:tblGrid>child element not present