windows下 python读取word中的表格报错
运行环境:
- python版本:3.7.0。
- python_docx版本:0.8.10
问题表述:使用 python 的 docx.Document 读取word文件。目标文件中有若干table表格,在某一个表格读取 row.cells 时报错,数组越界: IndexError: list index out of range。其他表格的数据均可正确读取。
求助目的:不改变word中表格的前提下,将该报错的表格信息读取出来。文件见下方。
代码如下:
from docx import Document
def read_word(docx_file):
document = Document(docx_file)
table_list = document.tables
if table_list:
for t_index, table in enumerate(table_list):
if table.rows:
for r_index, row in enumerate(table.rows):
for c_index, cell in enumerate(row.cells):
print(cell.text)
docx_file = "E:/test.docx"
read_word(docx_file)
文件如下:该文件中,只有1个表格,即:运行代码报错的表格。
wws.lanzous.com/iG8xujtzjna
推荐文章: