首页 > 编程语言 >python mac docx格式转pdf

python mac docx格式转pdf

时间:2022-12-22 18:11:42浏览次数:61  
标签:__ docx name python mac html file path os

1.翻了很多资料发现大多是Windows的库偏多,并且不支持linux,还需要借助word软件,方案是首先docx 转 html 再转 pdf

代码:

from pydocx import PyDocX
from xhtml2pdf import pisa
import os


def word2html(inputFile):
    with open(os.path.dirname(__file__) + '/' + inputFile.name, "wb") as f:
        for chunk in inputFile.chunks():
            f.write(chunk)

    file_path = os.path.dirname(__file__) + '/'
    file_input_path = file_path + inputFile.name
    html = PyDocX.to_html(file_input_path)
    outputFile = file_path + (inputFile.name.replace('.docx', '.html'))

    f = open(outputFile, 'w', encoding="utf-8")
    f.write(html)
    f.close()
    return outputFile


def convertHtmlToPdf(sourceHtml, outputFilename):
    data = open(sourceHtml).read()
    resultFile = open(outputFilename, "w+b")
    pisaStatus = pisa.CreatePDF(
        data,
        dest=resultFile)

    resultFile.close()
    return resultFile


def remove_resume_file(resume_file):
    for root, dirs, files in os.walk(os.path.dirname(__file__)):
        for name in files:
            if name.endswith(f"{resume_file}.docx") or name.endswith(f"{resume_file}.html") or name.endswith(f"{resume_file}.pdf"):
                os.remove(os.path.join(root, name))

 

标签:__,docx,name,python,mac,html,file,path,os
From: https://www.cnblogs.com/kylin5201314/p/16999341.html

相关文章