假设xmlTree是一个XML文档的ElementTree实例,可以使用以下代码来判断body、sectPr和pgBorders是否存在:

root = xmlTree.getroot()

# 判断body是否存在
if root.find('.//{http://schemas.openxmlformats.org/wordprocessingml/2006/main}body') is not None:
    print('body存在')
else:
    print('body不存在')

# 判断sectPr是否存在
if root.find('.//{http://schemas.openxmlformats.org/wordprocessingml/2006/main}sectPr') is not None:
    print('sectPr存在')
else:
    print('sectPr不存在')

# 判断pgBorders是否存在
if root.find('.//{http://schemas.openxmlformats.org/wordprocessingml/2006/main}pgBorders') is not None:
    print('pgBorders存在')
else:
    print('pgBorders不存在')

其中,.//{namespace}tag用于查找指定命名空间下的标签。在上面的代码中,{http://schemas.openxmlformats.org/wordprocessingml/2006/main}是WordprocessingML的命名空间。因此,.//{http://schemas.openxmlformats.org/wordprocessingml/2006/main}body表示查找命名空间为WordprocessingML,标签名为body的元素

xmlTreegetroot 如何判断 body sectPr pgBorders是否存在

原文地址: https://www.cveoy.top/t/topic/g7M4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录