python-如何在Libre Office中打开文件并将其保存为.doc文件?

提问

如何在Libre Office中打开文件并将其保存为.doc文件?有可能的? (为此创建脚本)

最佳答案

根据libreoffice manual(作为命令行实用程序),您不需要python,但是libreoffice应该直接支持此功能:

–convert-to output_file_extension[:output_filter_name] [–outdir output_dir] file… Batch converts files. If –outdir is not
specified then the current working directory is used as the output
directory for the convertedfiles.

Examples:

    –convert-to pdf *.doc

Converts all .doc files to PDFs.

    –convert-to pdf:writer_pdf_Export –outdir /home/user *.doc

Converts all .doc files to PDFs using the settings in the Writer PDF
export dialog and saving them in /home/user.

如果您需要处理许多文件,则可以编写简单的bash脚本,如下所示:

for i in `find folder -type f -name *.lwp` ; do
    libreoffice --headless --convert-to doc:"MS Word 2003 XML" $i
done

有关如何调用此命令here的更详细的说明,或在先前指定的手册中.

您基本上可以从python和subprocess进行相同的调用:

import os
import os.path
import subprocess

for i in os.listdir( SOURCE_FOLDER):
    if not i.endswith( '.lwp'):
        continue

    path = os.path.join( SOURCE_FOLDER, i)
    args = ['libreoffice', '--headless', '--convert-to',
            'doc:"MS Word 2003 XML"', path]

    subprocess.call(args, shell=False)