我有一个文档,其中包含以下示例文本,
This is a document which contains parent and child
• Photosynthesis
• CO~2~ in
• H~2~0 in
• Sunlight in
• Glucose
• CO~2~ in
• C~6~H~12~O~6~
• Oxygen out
如何在Python中分离这些父子项目,我用python-docx
import docx
doc = docx.Document('bullet_test.docx')
for p in doc.paragraphs:
text = p.text.encode("utf-8").strip()
style = p.style.name.strip()
print style
我得到的样式为'List Paragraph',我无法将其作为'List Bullet'或'List Bullet2'或'List Number'
您的子弹列表似乎使用“List Paragraph”样式进行样式设置,该样式是built-in Microsoft Word styles之一。并且,应用于每个段落的样式在本地修改以将其转换为子弹列表(不同级别)。所以你无法从风格本身猜测等级,你需要检查每个段落格式(子弹,制表等)。
使用段落的ParagraphFormat属性可用的paragraph_format对象访问段落的格式属性。