前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python图片转换pdf

python图片转换pdf

作者头像
py3study
发布2020-01-08 16:21:36
1.5K0
发布2020-01-08 16:21:36
举报
文章被收录于专栏:python3

#!/home/chao/anaconda3/envs/test_py2/bin/python #coding:utf-8 import os import sys from reportlab.lib.pagesizes import A4, landscape from reportlab.pdfgen import canvas from PIL import Image from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont #需要预告安装支持中文的字体,如simfang从win拷贝过来安装 def createPdf(dstpath,fileList):     img = Image.open( fileList[0].decode('UTF-8') )     c = canvas.Canvas(dstpath, img.size)#第一张图片的尺寸新建pdf     pdfmetrics.registerFont(TTFont('simfang','simfang.ttf')) #注册字体     fontheight=15     c.setFont('simfang',fontheight)     #c.drawString(100, 300, u'宋体宋体')     height=fontheight     num=1     for i in fileList:#标明本pdf的文件列表         c.drawString(fontheight,height,str(num)+"/"+str(len(fileList)))         c.drawString(fontheight+50, height, os.path.split(i)[1])         num+=1         height+=fontheight     c.showPage()     for i in fileList:         c.drawImage(i.decode('UTF-8'), 0, 0)#转换为中文路径名称打开         c.showPage()     c.save() def transferPdf(filePath,dstpath): #将一个目录下所有图片生成一个pdf     fileList=[]     #result=os.popen(" ls -l "+filePath+"| awk \'{print $9}\' | sort -t _ -k1,1 -k2n,2 ").read()     result=os.popen(" ls  "+filePath+"|  sort -t _ -k1,1 -k2n,2 ").read()     currentIndex=0     pdfIndex=0     for i in result.split("\n"):         if i.strip()!='':             print i             fileList.append(os.path.join(filePath, i))             currentIndex+=1             if currentIndex == 100:#每几页一创建                 currentIndex=0                 pdfIndex+=1                 createPdf( os.path.join(dstpath, str(pdfIndex)+".pdf") ,fileList)                 fileList=[] filePath = "/home/chao/img"#源图片文件夹 dstpath="/home/chao/tmp1"#转换出的pdf文件夹存放地址 transferPdf(filePath,dstpath)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档