【问题标题】:How to open file in Libre Office and save this like .doc file?如何在 Libre Office 中打开文件并将其保存为 .doc 文件?
【发布时间】:2013-04-24 21:01:10
【问题描述】:

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

【问题讨论】:

    标签: python libreoffice


    【解决方案1】:

    根据libreoffice manual(作为命令行实用程序)你不需要python,但libreoffice应该直接支持这个:

    --convert-to output_file_extension[:output_filter_name] [--outdir output_dir] file... 批量转换文件。如果 --outdir 不是 指定然后当前工作目录用作输出 转换文件的目录。

    例子:

        --convert-to pdf *.doc

    将所有 .doc 文件转换为 PDF。

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

    使用 Writer PDF 中的设置将所有 .doc 文件转换为 PDF 导出对话框并将它们保存在 /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)
    

    【讨论】:

      【解决方案2】:

      在 win7 上,使用 LO 4.1 我必须执行以下操作(从命令行,如果在 cmd 脚本中运行,您可能需要将 %f 转换为 %%f):

      set path=%path%;C:\Program Files (x86)\LibreOffice 4\program
      for %f in (*.odt) do (
          soffice.exe --headless --convert-to doc --outdir "C:\tmp" %f
      )
      

      注意事项:

      • 如果任何 LO 实例打开,它将不起作用!
      • 需要outdir
      • 不支持输入文件的通配符(因此使用 for 循环)

      相应的 python 脚本可能如下所示:

      import os
      import subprocess as sp
      
      lo = r'C:\Program Files (x86)\LibreOffice 4\program\soffice.exe'
      
      args = '--headless --convert-to doc --outdir "%(out)s" "%(inp)s"'
      
      inp_path = './odt'
      out_path = './doc'
      
      inp_path = os.path.normpath(os.path.abspath(inp_path))
      out_path = os.path.normpath(os.path.abspath(out_path))
      
      for root, dirs, files in os.walk(inp_path):
        for fname in files:
          if fname.endswith('.odt'):
            i = os.path.join(inp_path,fname)
            sp.call(lo + ' ' + args%{'out': out_path, 'inp': i})
      

      (从 ask.libreoffice 复制并修改了我的答案,也发布在 superuser

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-02
        相关资源
        最近更新 更多