Word 文档中的批注通常用于协作审阅和提供反馈意见。这些批注可能包含有价值的文本和图片信息。提取批注中的文本和图片内容,可以帮助作者分析和评估审阅者的意见,从而全面了解文档的优缺点及改进建议。本文将介绍如何使用 Spire.Doc for Python 在 Python 中提取 Word 文档批注中的文本和图片。
安装 Spire.Doc for Python
本教程需要 Spire.Doc for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Doc如果您不确定如何安装,请参考:如何在 Windows 中安装 Spire.Doc for Python
Python 从 Word 批注中提取文本
你可以使用 Spire.Doc for Python 提供的 Comment.Format.Author 和 Comment.Body.Paragraphs[index].Text 属性获取 Word 批注的作者和文本。详细步骤如下:
- 创建 Document 类的对象。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 创建一个列表来存储提取的批注数据。
- 遍历文档中的批注。
- 遍历每个批注中的段落。
- 使用 Comment.Body.Paragraphs[index].Text 属性获取每个段落的文本。
- 使用 Comment.Format.Author 属性获取批注的作者。
- 将批注的文本和作者添加到列表中。
- 将列表的内容保存到文本文件。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建一个 Document 类的对象
document = Document()
# 加载包含批注的 Word 文档
document.LoadFromFile("批注.docx")
# 创建一个列表来存储提取的批注数据
comments = []
# 遍历文档中的批注
for i in range(document.Comments.Count):
    comment = document.Comments[i]
    comment_text = ""
    # 遍历批注正文中的段落
    for j in range(comment.Body.Paragraphs.Count):
        paragraph = comment.Body.Paragraphs[j]
        comment_text += paragraph.Text + "\n"
    # 获取批注作者
    comment_author = comment.Format.Author
    # 将批注数据添加到列表中
    comments.append({
        "作者": comment_author,
        "内容": comment_text
    })
# 将批注数据写入文件
with open("批注.txt", "w", encoding="utf-8") as file:
    for i, comment in enumerate(comments, start=1):
        file.write(f"批注{i}:\n  作者: {comment['作者']}\n  批注内容: {comment['内容']}\n")
document.Close()
Python 从 Word 批注中提取图片
要从 Word 批注中提取图片,需要遍历批注段落中的子对象,找到 DocPicture 对象,然后使用 DocPicture.ImageBytes 属性获取图片数据,最后将图片数据保存为图片文件。
具体步骤如下:
- 创建 Document 类的对象。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 创建一个列表来存储提取的图片数据。
- 遍历文档中的批注。
- 遍历每个批注中的段落。
- 遍历每个段落的子对象。
- 检查对象是否为 DocPicture 对象。
- 如果对象是 DocPicture,使用 DocPicture.ImageBytes 属性获取图片数据,并将其添加到列表中。
- 将列表中的图片数据保存为单独的图片文件。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建一个 Document 类的对象
document = Document()
# 加载包含批注的 Word 文档
document.LoadFromFile("图片批注.docx")
# 创建一个列表来存储提取的图片数据
images = []
# 遍历文档中的批注
for i in range(document.Comments.Count):
    comment = document.Comments[i]
    # 遍历批注正文中的段落
    for j in range(comment.Body.Paragraphs.Count):
        paragraph = comment.Body.Paragraphs[j]
        # 遍历段落中的子对象
        for o in range(paragraph.ChildObjects.Count):
            obj = paragraph.ChildObjects[o]
            # 查找图片
            if isinstance(obj, DocPicture):
                picture = obj
                # 获取图片数据并添加到列表中
                data_bytes = picture.ImageBytes
                images.append(data_bytes)
# 将图片数据保存为图片文件
for i, image_data in enumerate(images):
    file_name = f"批注图片-{i}.png"
    with open(os.path.join("批注图片/", file_name), 'wb') as image_file:
        image_file.write(image_data)
document.Close()
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



