python html,docx类型转换成pdf

html 类型转换:

path_wkthmltopdf = r"D:\soft-tools\pdf-change\install\wkhtmltopdf\bin\wkhtmltopdf.exe"
def url_to_pdf(url, to_file):
    '''将网页生成pdf文件'''
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    # 生成pdf文件,to_file为文件路径
    pdfkit.from_url(url, to_file, configuration=config)
    print('完成')

def html_to_pdf(html, to_file):
    '''将html文件生成pdf文件'''
    # 将wkhtmltopdf.exe程序绝对路径传入config对象
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    # 生成pdf文件,to_file为文件路径
    pdfkit.from_file(html, to_file, configuration=config)
    print('完成')

def str_to_pdf(string, to_file):
    '''将字符串生成pdf文件'''
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    # 生成pdf文件,to_file为文件路径
    pdfkit.from_string(string, to_file, configuration=config)
    print('完成')


if __name__ == '__main__':
    url_to_pdf(r'http://www.csrc.gov.cn/csrc/c105891/c1806775/content.shtml', 'out_1.pdf')
    # html_to_pdf('sample.html', 'out_2.pdf')
    # str_to_pdf('我是中国人!', 'out_3.pdf')

doc,docx 转换成pdf:

import  os
from win32com import client
#pip instatll win32com
def doc2pdf(doc_name, pdf_name):
    """
    :word文件转pdf
    :param doc_name word文件名称
    :param pdf_name 转换后pdf文件名称
    """
    try:
        word = client.DispatchEx("Word.Application")
        if os.path.exists(pdf_name):
            os.remove(pdf_name)
        worddoc = word.Documents.Open(doc_name,ReadOnly = 1)
        worddoc.SaveAs(pdf_name, FileFormat = 17)
        worddoc.Close()
        return pdf_name
    except Exception as err:
        print(err)
        return 1
if __name__=='__main__':
    # doc_name = "D:\GIT_CODE\data_extract_python\s3file\804e0fc28fbe11ec8af01866da996334.docx"
    doc_name = "D:\GIT_CODE\data_extract_python\s3file\8001.doc"
    ftp_name = r"D:\GIT_CODE\data_extract_python\s3file\001.pdf"
    doc2pdf(doc_name, ftp_name)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值