亲宝软件园·资讯

展开

python图片转pdf Python把图片转化为pdf代码实例

阿磊小哥哥呀 人气:0

python安装reportlab

pip install reportlab -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

代码如下

# -*- coding: utf-8 -*-

from reportlab.lib.pagesizes import portrait
from reportlab.pdfgen import canvas
from PIL import Image


def jpg_to_pdf(jpg, pdf_path):
  (w, h) = Image.open(jpg).size
  user = canvas.Canvas(pdf_path, pagesize=portrait((w, h)))
  user.drawImage(jpg, 0, 0, w, h)
  user.showPage()
  user.save()


if __name__ == '__main__':
  jpg_path = 'code.png'
  pdf_path = 'code.pdf'
  jpg_to_pdf(jpg_path, pdf_path)

加载全部内容

相关教程
猜你喜欢
用户评论