【发布时间】:2023-10-13 04:19:01
【问题描述】:
我有一组带有“跟踪更改”功能的文本修改的 .docx 文档。
对于我集中的每个source.docx 文件,我想以编程方式运行两个操作:
- 生成两份文档,一份拒绝所有更改,另一份接受所有更改(棘手的步骤)
- 转换为纯文本。
换句话说,我想运行以下管道:
source.docx -> sources-all-changes-rejected.docx -> source-all-rejected-plaintext.txtsource.docx -> sources-all-changes-accepted.docx -> source-all-accepted-plaintext.txt
有没有办法做到这一点,例如使用soffice --headless?
我尝试了一个受 Python - Using win32com.client to accept all changes in Word Documents 启发的解决方案。该方法有效,注意使用绝对路径并保存为 txt 文档 https://docs.microsoft.com/en-us/office/vba/api/word.wdsaveformat 。所以我有一个函数,它采用pathlib 路径file_path 并根据需要编写纯文本文档:
def output_track_changed_version(file_path, action):
doc.TrackRevisions = False
# Delete all comments
if doc.Comments.Count >= 1:
doc.DeleteAllComments()
# Accept/reject all revisions
doc.Revisions.AcceptAll()
changed_text = doc.Content.Text
doc.Undo()
doc.Revisions.RejectAll()
original_text = doc.Content.Text
# [CUT: code to dump changed/original strings to file and then...]
doc.Close(False, False, False)
word.Application.Quit()
但我不想坚持使用win32com.client,我更喜欢基于 LibreOffice 的解决方案 + Python,可以在 Linux 虚拟机上轻松设置。
【问题讨论】:
标签: python ms-word docx libreoffice