转换docx。在Linux中的python中转换为pdf

时间:2020-07-16 09:38:16

标签: python-3.x linux libreoffice

它不会在目录中创建pdf文件,而且会打印-无

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吗?预先感谢!

1 个答案:

答案 0 :(得分:1)

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

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