#!/usr/bin/env python
import os
import glob
from reportlab.lib.pagesizes import letter, A4, landscape
from reportlab.platypus import SimpleDocTemplate, Image
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas
#filename = 'd:/tux.png'
#get_python_image()
def topdf(filename):
filename_pdf = filename[:-4] + '.pdf'
#im = Image(filename)
#doc = SimpleDocTemplate(filename_pdf, pagesize=landscape(A4))
#parts = []
#parts.append(im)
#doc.build(parts)
c = canvas.Canvas(filename_pdf, pagesize=landscape(A4))
(w, h) =landscape(A4)
width, height = letter
#c.drawImage(filename, inch, height - 2 * inch) # Who needs consistency?
c.drawImage(filename, 0, 0, w, h)
c.showPage()
c.save()
print "end."
path = os.path.abspath(os.path.join(os.getcwd(), __file__))
path = os.path.dirname(path)
print path
#topdf('D:\\4.jpg')
filelist = glob.glob(os.path.join(path, '*.jpg'))
for filename in filelist:
print filename
topdf(filename)
python圖片轉換成pdf
最新推荐文章于 2025-10-22 15:02:18 发布
本文介绍了一个使用Python实现的小程序,该程序能够将指定路径下的所有JPEG文件批量转换为PDF格式。利用ReportLab库进行PDF文件的生成,并实现了图片到PDF的完整布局。
735

被折叠的 条评论
为什么被折叠?



