ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python,多图片转换成pdf文件

2019-06-13 16:42:15  阅读:323  来源: 互联网

标签:__ 转换成 python self dirPath pdf top def pack


# -*- coding:utf-8 -*-
#!/usr/bin/env python

import os
from reportlab.lib.pagesizes import A4, landscape
from reportlab.pdfgen import canvas
from tkinter import *
import time

# 图片文件名称列表
IMAGEFILES = []
class pdfTk(object):

def __init__(self):
'''用于生成主界面用于填写'''
self.top = Tk()
self.sw = self.top.winfo_screenwidth()
self.sh = self.top.winfo_screenheight()
self.topw = 500
self.toph = 200
self.top.title('图片转pdf生成器')
self.top.geometry("%dx%d+%d+%d" % (self.topw, self.toph, (self.sw - self.topw) / 2, (self.sh - self.toph) / 2))

self._DIRPATH = StringVar(self.top)

self.emptfmone = Frame(self.top, height=50)
self.emptfmone.pack()

self.dirfm = Frame(self.top)
self.descriptLabel = Label(self.dirfm, width=4, text='路径:')
self.descriptLabel.pack(side=LEFT)
self.dirn = Entry(self.dirfm, width=50, textvariable=self._DIRPATH)
#self.dirn.bind('<Return>', self.setPath)
self.dirn.pack(side=LEFT)
self.dirfm.pack()

self.emptfmtwo = Frame(self.top, height=30)
self.emptfmtwo.pack()

self.btnfm = Frame(self.top)
self.converBtn = Button(self.btnfm, width=10, text='生成PDF', command=self.doneAnyThing,
activeforeground='white', activebackground='blue')
self.quitBtn = Button(self.btnfm, width=10, text='退出', command=self.top.quit, activeforeground='white',
activebackground='blue')
self.converBtn.pack(side=LEFT, padx=10)
self.quitBtn.pack(side=LEFT, padx=10)
self.btnfm.pack()

def doneAnyThing(self):
self.getListImages(self._DIRPATH.get())
pdfFile = self.converPath(self._DIRPATH.get()) + self.dateStr() + ".pdf"
self.convertpdf(pdfFile)

def convertpdf(self, pdfFile):
'''多个图片合成一个pdf文件'''
(w, h) = landscape(A4) #
cv = canvas.Canvas(pdfFile, pagesize=landscape(A4))
for imagePath in IMAGEFILES:
cv.drawImage(imagePath, 0, 0, w, h)
cv.showPage()
cv.save()

def getListImages(self, dirPath):
'''读取指定文件夹下所有的JPEG图片,存入列表'''
if dirPath is None or len(dirPath) == 0:
raise ValueError('dirPath不能为空,该值为存放图片的具体路径文件夹!')
if os.path.isfile(dirPath):
raise ValueError('dirPath不能为具体文件,该值为存放图片的具体路径文件夹!')
if os.path.isdir(dirPath):
for imageName in os.listdir(dirPath):
if imageName.endswith('.jpg') or imageName.endswith('.jpeg'):
absPath = self.converPath(dirPath) + imageName
IMAGEFILES.append(absPath)

def converPath(self, dirPath):
'''用于转换路径,判断路径后是否为\\,如果有则直接输出,如果没有则添加'''
if dirPath is None or len(dirPath) == 0:
raise ValueError('dirPath不能为空!')
if os.path.isfile(dirPath):
raise ValueError('dirPath不能为具体文件,该值为文件夹路径!')
if not str(dirPath).endswith("\\"):
return dirPath + "\\"
return dirPath

def dateStr(self):
'''用于生成指定格式的日期,目的是为了拼接字符串'''
return time.strftime("%Y-%m-%d", time.localtime())

def main():
'''该函数主要用于生成PDF文件'''
pdfTk()
mainloop()

if __name__ == '__main__':
'''主函数,进行启动'''
main()

标签:__,转换成,python,self,dirPath,pdf,top,def,pack
来源: https://www.cnblogs.com/zxf-study/p/11017396.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有