#!/usr/bin/python3.6 # -*- coding: utf-8 -*- # @Time : 2021/3/15 9:19 # @Author : lijunliang # @Email : lijunliang@topnet.net.cn # @File : convert2pdf.py # @Software: PyCharm import pdfkit import subprocess from loguru import logger import os def html2pdf(html: str, pdf_path: str): try: pdfkit.from_string(html, pdf_path) except Exception as e: logger.warning("html转pdf失败--->%s" % e) return False return True def lib2pdf(file_path: str): ''' doc,docx :param file_path: str :return: ''' file_path = file_path.strip() out_dir = os.path.dirname(file_path) try: args = 'soffice --headless --convert-to pdf:writer_pdf_Export %s --outdir %s' % (file_path, out_dir) output = subprocess.check_output(args, shell=True) except subprocess.CalledProcessError as e: logger.warning('转pdf出错---->%s' % e) return '' filePathList = file_path.split(".") filePathList[-1] = "pdf" pdfFileName = ".".join(filePathList) return pdfFileName if __name__ == '__main__': # pdf_path = "../data/0.pdf" # html = open("../data/test.htm", "r").read() # pdf_data = html2pdf(html,pdf_path) # print(pdf_data) file_path = "../data/123.docx" pdf = lib2pdf(file_path) print(pdf)