【问题标题】:Replacing a phrase in a `.doc` file with python用python替换`.doc`文件中的短语
【发布时间】:2021-06-09 11:35:31
【问题描述】:

我有一个包含文本的 .doc 文件,我需要用此文档中的另一个短语替换特定短语。

我尝试使用python-docx,但它不支持.doc 格式。我也尝试使用普通的字符串替换功能,但它破坏了 doc 文件

with open("input.doc") as r:
   text = r.read().replace("old text", "new text")
with open("output.doc", "w") as w:
   w.write(text)

我无法更改文件的扩展名,我想在 python 中进行。

【问题讨论】:

标签: python linux ms-word python-docx .doc


【解决方案1】:

我认为你必须以二进制模式打开文件:

open("input.doc", "rb")

open("output.doc", "wb")

您必须更改“替换”方法以使其适合二进制模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2021-02-02
    • 2013-03-11
    相关资源
    最近更新 更多