在 Python 中读取 .docx 文件以查找删除线、项目符号和其他格式 [英] Reading .docx files in Python to find strikethrough, bullets and other formats
问题描述
谁能帮我在 Python 中使用 python-docx 识别 .docx 文件中的一个段落是否包含带有删除线格式的文本(即它出现但被划掉),或者在开头有一个项目符号?我正在尝试编写一个脚本来识别文档中的结构并解析内容.
Can anyone help me identify, in Python using python-docx, if a paragraph in a .docx file contains text that is formatted with strikethrough (ie. it appears but is crossed out), or has a bullet point at the start? I am trying to write a script to identify the structure in a document and parse the content.
到目前为止,我能够读取 .docx 文件并遍历段落,识别粗体段落.
So far I am able to read a .docx file and iterate over the paragraphs, identifying paragraphs that are bold.
from docx import Document
document = Document(r'C:\stuff\Document.docx')
for p in document.paragraphs:
print p.text
for run in p.runs:
if run.bold:
print 'BOLD ' + run.text
其他的我暂时不知道.
推荐答案
对于删除线,你可以像这样修改你的例子:
For strikethrough, you can just modify your example like so:
from docx import Document
document = Document(r'C:\stuff\Document.docx')
for p in document.paragraphs:
for run in p.runs:
if run.font.strike:
print "STRIKE: " + run.text
请参阅 API 文档以了解 字体 可以查看更多有趣内容的对象.
See the API docs for the Font object for more fun stuff you can check.
这篇关于在 Python 中读取 .docx 文件以查找删除线、项目符号和其他格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!