Python3将jpg转为pdf文件的方法示例
作者:nudt_qxx 发布时间:2021-06-25 11:31:17
标签:Python3,jpg,pdf
本文实例讲述了Python3将jpg转为pdf文件的方法。分享给大家供大家参考,具体如下:
#coding=utf-8
#!/usr/bin/env python
"""
convert image to pdf file
"""
#Author: mrbeann
import os
import sys
import glob
import platform
from reportlab.lib.pagesizes import letter, A4, landscape
from reportlab.platypus import SimpleDocTemplate, Image
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas
from reportlab import rl_settings
from PIL import Image
import importlib,sys
#importlib.reload(sys)
#sys.setdefaultencoding("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 platform.system() == 'Windows':
path = path.replace('\\','/')
if path[-1] != '/':
path = (path + '/')
if recursion == True:
for i in os.listdir(path):
if os.path.isdir(os.path.abspath(os.path.join(path, i))):
topdf(path+i,recursion,pictureType,sizeMode,width,height,fit,save)
filelist = []
if pictureType == None:
filelist = glob.glob(os.path.join(path, '*.jpg'))
else:
for i in pictureType:
filelist.extend(glob.glob(os.path.join(path, '*.'+i)))
maxw = 0
maxh = 0
if sizeMode == None or sizeMode == 0:
for i in filelist:
im = Image.open(i)
if maxw < im.size[0]:
maxw = im.size[0]
if maxh < im.size[1]:
maxh = im.size[1]
elif sizeMode == 1:
maxw = 999999
maxh = 999999
for i in filelist:
im = Image.open(i)
if maxw > im.size[0]:
maxw = im.size[0]
if maxh > im.size[1]:
maxh = im.size[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 + path.split('/')[-2]
else:
filename_pdf = save + path.split('/')[-2]
filename_pdf = filename_pdf + '.pdf'
c = canvas.Canvas(filename_pdf, pagesize=maxsize )
l = len(filelist)
for i in range(l):
(w, h) =maxsize
width, height = letter
if fit == True:
c.drawImage(filelist[i] , 0,0)
else:
c.drawImage(filelist[i] , 0,0,maxw,maxh)
c.showPage()
c.save()
def main():
topdf(u'F:/gitplace/jpg2pdf/test',pictureType=['png','jpg'],save='F:/gitplace/jpg2pdf/test/新建文件夹')
if __name__ == '__main__':
main()
GitHub地址:https://github.com/mrbeann/jpg2pdf
更多Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/xiangxianghehe/article/details/100716837
0
投稿
猜你喜欢
- 记一次在写cli脚本的时候,碰到的一个问题。问题自己是写服务端的,有时候会写一些cli脚本去跑测试。习惯main.go写主流程,其他子文件写
- 我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval。ceil -- 进一法取整说明float ceil (
- 本文实例讲述了Python实现方便使用的级联进度信息的方法。分享给大家供大家参考。具体实现方法如下:class StepedProgress
- 一、简介 多线程编程技术可以实现代码并行性,优化处理能力,同时功能的
- sort()方法排序列表中的对象,比较使用func(如果给定)。语法以下是sort()方法的语法:list.sort([func
- 背景我们经常调侃程序员每天都在写bug,这确实是事实,没有测出bug不代表程序就真的不存在问题。传统的代码review、静态分析、人工测试和
- 用Python实现点对点的聊天,2个程序,一个是client.py,一个是server.py,通过本机地址127.0.0.1连接进行通信,利
- 任何一位数据库程序员都会有这样的体会:高通信量的数据库驱动程序中,一条糟糕的SQL查询语句可对整个应用程序的运行产生严重的影响,其不仅消耗掉
- 微软上周发布了一份关于 Windows Internet Explorer 8 浏览器(以下简称为 IE 8)性能优化的白皮书《Window
- 又有人说设session.timeout=99999。这种同样不行,session有最大时间限制。我经过测试发现最大值为24小时,也就是说你
- 下面十条内容的标题原本是《10 Lessons for Young Designers》,是John C. Jay给年青设计师们的十条经验教
- 一、ASP中处在双引号中的可以是任意的字符、字符串,HTML代码1.<%response.write ("I&nb
- 我们以MNIST手写数字识别为例import numpy as npfrom keras.datasets import mnistfrom
- 现在因为已经安装了2.6的Python,以及支持2.6的Eric4,就不想再重新安装2.5来继续配置Apache下mod_python了。后
- 问:如何给导入文件加上时间戳标记?答:请参考下文中介绍的两种方法:1.在DOS下从系统获得时间戳利用Dos命令取得时间戳:C:\>ec
- 代码如下:SELECT [StartDate] FROM [dbo].[udf_Week](2012,2012) WHERE [
- 数据共享是数据库最基本的特征之一。但是数据共享虽然为员工带来了便利,但也产生了一些负面作用。例如因用户并发存取而导致的对数据一致性的破坏、由
- 微信小程序与php 实现微信支付的简单实例小程序访问地址:payfee.PHP:include 'WeixinPay.php'
- 介绍psutil能够轻松实现获取系统运行的进程和系统利用率。导入模块import psutils获取系统性能信息CPU信息使用cpu_tim
- 运行环境: python 3.6.0今天处于练习的目的,就用 python 写了一个百度翻译,是如何做到的呢,其实呢就是拿到接口,通过这个接