python图片转换pdf
短信预约 -IT技能 免费直播动态提醒
#!/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)
#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)
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341