广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python实现网页文件转PDF文件和PNG图片的示例代码
  • 431
分享到

Python实现网页文件转PDF文件和PNG图片的示例代码

2024-04-02 19:04:59 431人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录一、html网页文件转pdf二、html网页文件转png一、html网页文件转pdf #将HTML文件导出为PDF def html_to_pdf(html_path,pdf_p

一、html网页文件转pdf

#将HTML文件导出为PDF
def html_to_pdf(html_path,pdf_path='.\\pdf_new.pdf',html_encoding='UTF-8',path_wkpdf = r'.\Tools\wkhtmltopdf.exe'):
    '''
    将HTML文件导出为PDF
    
    :param html_path:str类型,目标HTML文件的路径,可以是一个路径,也可以是多个路径,以list方式传入路径;或者一个或者多个网址;或者为一个字符串
    
    :param pdf_path:str类型,需要导出的PDF文件的路径
    
    :param html_encoding:str类型,html的编码格式,具体要看html页面到底是以什么编码格式保存的
    
    :param path_wkpdf:str类型,path_wkpdf = r'.\Tools\wkhtmltopdf.exe'  # 工具路径
    :return:
    '''
    cfg = pdfkit.configuration(wkhtmltopdf=path_wkpdf)
    options = {
        "encoding": html_encoding  # 这个具体要看html页面到底是以什么编码格式保存的
    }
 
    if 'Http' in str(html_path) and ('html' not in str(html_path) or 'HTML' not in str(html_path)):     #判断是否为非网址
        #从url获取html,再转为pdf
        print('http=>pdf')
        # pdfkit.from_url('https://httpbin.org/ip', 'ip.pdf', options=options, configuration=cfg)
        # pdfkit.from_url(['https://httpbin.org/ip', 'https://httpbin.org/ip'], 'ip.pdf', options=options,configuration=cfg)  # 传入列表
        pdfkit.from_url(html_path, pdf_path, options=options, configuration=cfg)
        
    elif 'html' in str(html_path) or 'HTML' in str(html_path):          #判断是否为HTML文件
        #将html文件转为pdf
        print('html,str=>pdf')
        # pdfkit.from_file(r'./helloworld.html', 'helloworld.pdf',options=options,  configuration=cfg)
        pdfkit.from_file(html_path, pdf_path, options=options, configuration=cfg)
        
    elif isinstance(html_path, list) and ('html' in str(html_path) or 'HTML' in str(html_path)):   #判断html目标是否为list,
        # 如:[r'./helloworld.html', r'./111.html', r'./222.html']
        print('html,list=>pdf')
        pdfkit.from_file(html_path, pdf_path,options=options,  configuration=cfg)  # 传入列表
    
    else:
        #将字符串转为pdf
        print('from_string=>pdf')
        pdfkit.from_string(html_path, pdf_path,options=options,  configuration=cfg)

所需要用的附件程序:

wkhtmltopdf.exe

下载地址

二、html网页文件转png

#将HTML文件导出为图片
def html_to_png(html_path,pdf_path='.\\pdf_new.pdf',html_encoding='UTF-8',path_wkpdf = r'.\Tools\wkhtmltoimage.exe'):
    '''
    将HTML文件导出为图片
    
    :param html_path:str类型,目标HTML文件的路径,可以是一个路径,也可以是多个路径,以list方式传入路径;或者一个或者多个网址;或者为一个字符串
    
    :param pdf_path:str类型,需要导出的图片文件的路径
    
    :param html_encoding:str类型,html的编码格式,具体要看html页面到底是以什么编码格式保存的
    
    :param path_wkpdf:str类型,path_wkpdf = r'.\Tools\wwkhtmltoimage.exe'  # 工具路径
    :return:
    '''
    cfg = imgkit.config(wkhtmltoimage=path_wkpdf)
    options = {
        "encoding": html_encoding  # 这个具体要看html页面到底是以什么编码格式保存的
    }
 
    if 'http' in str(html_path) and ('html' not in str(html_path) or 'HTML' not in str(html_path)):     #判断是否为非网址
        #从url获取html,再转为pdf
        print('http=>png')
        # pdfkit.from_url('https://httpbin.org/ip', 'ip.png', options=options, configuration=cfg)
        # pdfkit.from_url(['https://httpbin.org/ip', 'https://httpbin.org/ip'], 'ip.png', options=options,configuration=cfg)  # 传入列表
        imgkit.from_url(html_path, pdf_path, options=options, config=cfg)
        
    elif 'html' in str(html_path) or 'HTML' in str(html_path):          #判断是否为HTML文件
        #将html文件转为pdf
        print('html,str=>png')
        # pdfkit.from_file(r'./helloworld.html', 'helloworld.png',options=options,  configuration=cfg)
        imgkit.from_file(html_path, pdf_path, options=options, config=cfg)
        
    elif isinstance(html_path, list) and ('html' in str(html_path) or 'HTML' in str(html_path)):   #判断html目标是否为list,
        # 如:[r'./helloworld.html', r'./111.html', r'./222.html']
        print('html,list=>png')
        imgkit.from_file(html_path, pdf_path,options=options,  config=cfg)  # 传入列表
    
    else:
        #将字符串转为pdf
        print('from_string=>png')
        imgkit.from_string(html_path, pdf_path,options=options,  config=cfg)

所需要用的附件程序:

wkhtmltoimage.exe

下载地址

到此这篇关于python实现网页文件转PDF文件和PNG图片的示例代码的文章就介绍到这了,更多相关Python网页文件转PDF PNG内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Python实现网页文件转PDF文件和PNG图片的示例代码

本文链接: https://www.lsjlt.com/news/162369.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作