赞
踩
- pip install imgkit
- pip install pdfkit
下载地址:https://wkhtmltopdf.org/downloads.html
下载之后安装,安装完成会生成两个程序,分别用来转图片和pdf:
- import imgkit
-
-
- path_wkimg = r'C:\Program Files\wkhtmltopdf/bin\wkhtmltoimage.exe' # 工具路径
- cfg = imgkit.config(wkhtmltoimage=path_wkimg)
-
- # 1、将html文件转为图片
- imgkit.from_file(r'./helloworld.html', 'helloworld.jpg', config=cfg)
-
- # 2、从url获取html,再转为图片
- imgkit.from_url('https://httpbin.org/ip', 'ip.jpg', config=cfg)
-
- # 3、将字符串转为图片
- imgkit.from_string('Hello!','hello.jpg', config=cfg)
- import pdfkit
-
-
- path_wkpdf = r'C:\Program Files\wkhtmltopdf/bin\wkhtmltopdf.exe' # 工具路径
- cfg = pdfkit.configuration(wkhtmltopdf=path_wkpdf)
-
- # 1、将html文件转为pdf
- pdfkit.from_file(r'./helloworld.html', 'helloworld.pdf', configuration=cfg)
- # 传入列表
- pdfkit.from_file([r'./helloworld.html', r'./111.html', r'./222.html'], 'helloworld.pdf', configuration=cfg)
-
- # 2、从url获取html,再转为pdf
- pdfkit.from_url('https://httpbin.org/ip', 'ip.pdf', configuration=cfg)
- # 传入列表
- pdfkit.from_url(['https://httpbin.org/ip','https://httpbin.org/ip'], 'ip.pdf', configuration=cfg)
-
- # 3、将字符串转为pdf
- pdfkit.from_string('Hello!','hello.pdf', configuration=cfg)
- import imgkit
-
-
- options = {'encoding': 'utf8'}
- imgkit.from_string(table, 'table.jpg',options=options)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。