Python 实现 pdf转图片 和 图片转pdf

1.所需要的环境和工具包

PyMuPDF,reportlab,PIL
import os
import sys
import fitz
from reportlab.lib.pagesizes import portrait
from reportlab.pdfgen import canvas
from PIL import Image

2. pdf转图片

def pdf2img(filename):
	#  打开PDF文件,生成一个对象
	doc = fitz.open(filename)
	print("共",doc.pageCount,"页")
	for pg in range(doc.pageCount):
		print("\r转换为图片",pg+1,"/",doc.pageCount,end="")
		page = doc[pg]
		rotate = int(0)
		# 每个尺寸的缩放系数为8,这将为我们生成分辨率提高64倍的图像。一般设为2
		zoom_x = 8.0
		zoom_y = 8.0
		trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
		pm = page.getPixmap(matrix=trans, alpha=False)
		pm.writePNG(filename+'_tu'+'{:02}.png' .format(pg))
	print()

if __name__ == '__main__':
    filename='0.pdf'
    pdf2img(filename)

3.单张图片转pdf

def img2pdf(img_name,outputpath):
	(maxw, maxh) = Image.open(img_name).size
	c = canvas.Canvas(outputpath, pagesize=portrait((maxw, maxh)))
	c.drawImage(img_name, 0, 0, maxw, maxh)
	c.showPage()
	c.save()
if __name__ == "__main__":
	img2pdf(img_name='E:\wm_workspace\pdf2img\data\实习证明.png',outputpath='E:\wm_workspace\pdf2img\data\实习证明.pdf')
	print("\n转换完成!")

4.多张图片转成一个pdf

from reportlab.platypus import SimpleDocTemplate,  PageBreak
from reportlab.platypus import Image as Image_reportlab
from reportlab.lib.pagesizes import A4, landscape
def mutil_img2pdf(save_pdf_path, img_dir):
    '''
    开始转换
    参数:
        save_pdf_path : 保存的pdf文件路径
        img_dir: 图片目录
    '''
    img_paths=[]
    img_names=os.listdir(img_dir)
    for imgname in img_names:
        img_path=os.path.join(img_dir,imgname)
        img_paths.append(img_path)
    # A4 纸的宽高
    __a4_w, __a4_h = landscape(A4)

    # 对数据进行排序
    img_paths.sort()

    bookPagesData = []

    # 创建一个简单模板
    bookDoc = SimpleDocTemplate(save_pdf_path)

    for img_path in img_paths:
        # 获取图片的宽和高
        img_pil= Image.open(img_path)
        img_w,img_h=img_pil.size[0],img_pil.size[1]
        # 取合适的比例
        if __a4_w / img_w < __a4_h / img_h:
            ratio = __a4_w / img_w
        else:
            ratio = __a4_h / img_h

        data = Image_reportlab(img_path, img_w * ratio, img_h * ratio)
        bookPagesData.append(data)
        bookPagesData.append(PageBreak())
    bookDoc.build(bookPagesData)

你可能感兴趣的:(文本处理,工具,python)