如何使用反词将多个.doc文件转换为.docx?

此手动命令正在工作:

!antiword "test" > "test.docx"

但是以下脚本将文件转换为空的.docx文件:

for file in os.listdir(directory):
    subprocess.run(["bash", "-c", "antiword \"$1\" > \"$1\".docx", "_", file])

还将.docx文件存储在\ a \ b中的上一个直接e-g文件中,此命令会将文件存储到\ a

我尝试了许多不同的方法,包括直接在终端和bash循环上运行。只有手动方式有效

评论
  • qqgsd
    qqgsd 回复

    Something like this should work (adjust dest_path etc. accordingly).

    import os
    import shlex
    
    for filename in os.listdir(directory):
        if ".doc" not in filename:
            continue
        path = os.path.join(directory, filename)
        dest_path = os.path.splitext(path)[0] + ".txt"
        cmd = "antiword %s > %s" % (shlex.quote(path), shlex.quote(dest_path))
        print(cmd)
        # If the above seems to print correct commands, add:
        # os.system(cmd)