转换 .docx。在 python 中转为 pdf 在 linux 中

Convert docx. to pdf in python in linux

它不会在打印的目录中创建 pdf 文件 - None

import sys
import subprocess
import re


def convert_to(folder, source, timeout=None):
    args = [libreoffice_exec(), '--headless', '--convert-to', 'pdf', '--outdir', folder, source]

    process = subprocess.run(args, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE, timeout=timeout)
    filename = re.search('-> (.*?) using filter', process.stdout.decode())

    return filename


def libreoffice_exec():
    # TODO: Provide support for more platforms
    if sys.platform == 'darwin':
        return '/Applications/LibreOffice.app/Contents/MacOS/soffice'
    return 'libreoffice'

result = convert_to('/directory_to_save_file',  '/File_path', timeout=15)
print(result)

谁能给我一个可以轻松将 docx 转换为 pdf 的解决方案?提前致谢!

您可以在您的函数中尝试以下代码

import subprocess
output = subprocess.check_output(['libreoffice', '--convert-to', 'pdf' ,'demo.docx'])
print output