Python:未检测word文档中表的某些单元格

2024-05-29 10:52:30 发布

您现在位置:Python中文网/ 问答频道 /正文

import sys 
import re 
import os 
import docx
from docx import Document
f_name = "./List.docx"
try:
    wordDoc = Document(f_name)
except IOError:
    print ("\n*** %s : Word Document NOT found.\n"%(f_name))
tables= wordDoc.tables
for t in range(0,9,1):
    print (tables[1].rows[t].cells[0].text)

please click this to see the table from the word document

打印语句结果如下

item
ham
glass
ring
keys
75
1
Juice
coffee

如您所见,它没有检测到某些单元格[0]元素。而是为第5行、第7行打印单元格1或单元格[2]。跳过第6行和第8行。我使用的是python 3.7.4,我的word文档版本是Microsoft word 2013(15.0.5197.1000)MSO(15.0.5172.1000)32位。请告诉我修理的方法


Tags: namefromimportretablesossysdocument

热门问题