SoFunction
Updated on 2024-11-19

Python3 will be converted to pdf file jpg method sample

In this paper, the example of Python3 will be converted to pdf file jpg method. Shared for your reference, as follows:

#coding=utf-8
#!/usr/bin/env python
"""
convert image to pdf file
"""
#Author: mrbeann 
import os
import sys
import glob
import platform
from  import letter, A4, landscape
from  import SimpleDocTemplate, Image
from  import inch
from  import canvas
from reportlab import rl_settings
from PIL import Image
import importlib,sys
#(sys)
#("utf-8")
def topdf(path,recursion=None,pictureType=None,sizeMode=None,width=None,height=None,fit=None,save=None):
  """
  Parameters
  ----------
  path : string
      path of the pictures
  recursion : boolean
        None or False for no recursion
        True for recursion to children folder
        wether to recursion or not
  pictureType : list
         type of pictures,for example :jpg,png...
  sizeMode : int
      None or 0 for pdf's pagesize is the biggest of all the pictures
      1 for pdf's pagesize is the min of all the pictures
      2 for pdf's pagesize is the given value of width and height
      to choose how to determine the size of pdf
  width : int
      width of the pdf page
  height : int
      height of the pdf page
  fit : boolean
      None or False for fit the picture size to pagesize
      True for keep the size of the pictures
      wether to keep the picture size or not
  save : string
      path to save the pdf
  """
  if () == 'Windows':
    path = ('\\','/')
  if path[-1] != '/':
    path = (path + '/')
  if recursion == True:
    for i in (path):
      if (((path, i))):
        topdf(path+i,recursion,pictureType,sizeMode,width,height,fit,save)
  filelist = []
  if pictureType == None:
    filelist = ((path, '*.jpg'))
  else:
    for i in pictureType:
      (((path, '*.'+i)))
  maxw = 0
  maxh = 0
  if sizeMode == None or sizeMode == 0:
    for i in filelist:
      im = (i)
      if maxw < [0]:
        maxw = [0]
      if maxh < [1]:
        maxh = [1]
  elif sizeMode == 1:
    maxw = 999999
    maxh = 999999
    for i in filelist:
      im = (i)
      if maxw > [0]:
        maxw = [0]
      if maxh > [1]:
        maxh = [1]
  else:
    if width == None or height == None:
      raise Exception("no width or height provid")
    maxw = width
    maxh = height
  maxsize = (maxw,maxh)
  if save == None:
    filename_pdf = path + ('/')[-2]
  else:
    filename_pdf = save + ('/')[-2]
  filename_pdf = filename_pdf + '.pdf'
  c = (filename_pdf, pagesize=maxsize )
  l = len(filelist)
  for i in range(l):
    (w, h) =maxsize
    width, height = letter
    if fit == True:
      (filelist[i] , 0,0)
    else:
      (filelist[i] , 0,0,maxw,maxh)
    ()
  ()
def main():
  topdf(u'F:/gitplace/jpg2pdf/test',pictureType=['png','jpg'],save='F:/gitplace/jpg2pdf/test/new folder')
if __name__ == '__main__':
  main()

GitHub Address:/mrbeann/jpg2pdf

For more Python related content, readers can check out this site's topic:Summary of Python file and directory manipulation techniques》、《Summary of Python coding manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.