对目录内的jpg文件组合成一个pdf文件

2020年上半年,疫情期间,学生在家上课,需要电子课本,就把网上下载的凤凰电子课本做成pdf的,供学生使用,希望对您有帮助。

 

import os
from reportlab.lib.pagesizes import A4, landscape,portrait
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
from tkinter import *
import time
'''
该程序把图片加到pdf里,并保存




'''
pdfmetrics.registerFont(TTFont('SimSun', 'SimSun.ttf')) #注册字体
# 图片文件名称列表
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 convertpdf(self, pdfFile):
      '''多个图片合成一个pdf文件'''
      (w, h) = portrait(A4) #
      cv = canvas.Canvas(pdfFile, pagesize=portrait(A4))
      for imagePath in IMAGEFILES:

         cv.drawImage(imagePath, 0, 0, w, h)
         cv.setFont(psfontname='SimSun', size = 14)
         cv.setFillColor('#0000ff')
         cv.drawString(20, 20,'外校收集,不得用于商业')
         cv.showPage()
      cv.save()
      IMAGEFILES.clear()

   def doneAnyThing(self):
         dirPath=r'F:\电子教材'

         if os.path.isdir(dirPath):
            t = os.listdir(dirPath)

            for itempath in t:
               print(dirPath+'\\'+itempath)
               self.getListImages(dirPath+'\\'+itempath)
               pdfFile = self.converPath(dirPath+'\\'+itempath) + itempath + ".pdf"
               self.convertpdf(pdfFile)


# self.getListImages(self._DIRPATH.get())
# pdfFile = self.converPath(self._DIRPATH.get()) + self.dateStr() + ".pdf"
# self.convertpdf(pdfFile)
   '''
   原来的
      def doneAnyThing(self):
         self.getListImages(self._DIRPATH.get())
         pdfFile = self.converPath(self._DIRPATH.get()) + self.dateStr() + ".pdf"
         self.convertpdf(pdfFile)
   '''

   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):
         t=os.listdir(dirPath)
         imgnum=len(t)

         for imgitem in range(1,imgnum+1) :
               absPath = self.converPath(dirPath) + str(imgitem)+'.jpg'
               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()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值