python pdf转图片,图片转pdf[2023-3-26日更新]
本帖最后由 sygde 于 2023-3-26 19:41 编辑首先安装两个模块
pip install fitz
pip install Pillow
pip install PyMuPDF
执行代码前,先在执行文件当前目录创建pdf和img两个文件夹
可批量将pdf转为图片文件
import fitz
import os
'''
# 将PDF转化为图片
pdfPath pdf文件的路径
imgPath 图像要保存的文件夹
zoom_x x方向的缩放系数
zoom_y y方向的缩放系数
rotation_angle 旋转角度
'''
def pdf_image(pdfPath,imgPath,zoom_x,zoom_y,rotation_angle):
#获取pdf文件名
files = os.listdir(folder)
print(files)
#创建对应pdf文件的img文件夹
#1.设置保存的img文件路径
#2.获取pdf文件路径
#3.打开pdf文件
#4.读取PDF文件
#5.保存img文件
for file in files:
#设置img文件路径
saveImgPath = imgPath + file + '/'
#创建对应pdf文件的img文件夹
isExists=os.path.exists(saveImgPath)
# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(saveImgPath)
#获取pdf文件路径
pdfPath = folder + file
# 打开PDF文件
pdf = fitz.open(pdfPath)
# 逐页读取PDF
for pg in range(0, pdf.page_count):
page = pdf
# 设置缩放和旋转系数,zoom_x和zoom_y为缩放倍数
trans = fitz.Matrix(zoom_x, zoom_y).prerotate(rotation_angle)
pm = page.get_pixmap(matrix=trans, alpha=False)
# 开始写图像
pm.save(saveImgPath+str(pg)+".png")
pdf.close()
curdir = os.path.dirname(__file__)
folder = curdir + "/pdf/" #生成pdf的文件目录
imgFile = curdir + "/img/" #png图片目录
pdf_image(folder,imgFile,1,1,0)
执行结果
将图片转为pdf,支持png和jpg
如对顺序有要求,可将图片按照顺序改名为从0开始的数字名称。
在执行代码前,先在该目录创建img文件夹,将要转换的图片放到img文件夹当中,转换的pdf文件,会以1.pdf的文件存放在img文件夹当中。
from PIL import Image
import os
Image.MAX_IMAGE_PIXELS = None
def combine2Pdf( folderPath, pdfFilePath ):
files = os.listdir( folderPath )
pngFiles = []
sources = []
exts = ['png','jpg']
for file in files:
for ext in exts:
if ext in file:
pngFiles.append( folderPath + file )
pngFiles.sort()
output = Image.open( pngFiles )
pngFiles.pop( 0 )
for file in pngFiles:
pngFile = Image.open( file )
if pngFile.mode == "RGB":
pngFile = pngFile.convert( "RGB" )
sources.append( pngFile )
output.save( pdfFilePath, "pdf", save_all=True, append_images=sources )
if __name__ == "__main__":
curdir = os.path.dirname(__file__)
# folder = input("请输入图片目录:")
# pdfFile = input("请输入输出的pdf目录:")
folder = curdir + "/img/" #png图片目录
pdfFile = curdir + "/img/result.pdf" #生成pdf的文件目录
combine2Pdf( folder, pdfFile )
执行结果
zhcj66 发表于 2022-5-22 21:16
不知道下面这些错误如何解决
Traceback (most recent call last):
我也出现的这个问题,安装了 pip install PyMuPDF 这个,然后就可以用了,
Deprecation: 'preRotate' removed from class 'Matrix' after v1.19 - use 'prerotate'.
Deprecation: 'getPixmap' removed from class 'Page' after v1.19 - use 'get_pixmap'.
Deprecation: 'writePNG' removed from class 'Pixmap' after v1.19 - use 'save'.
把这几个也换了 不知道下面这些错误如何解决
Traceback (most recent call last):
File "D:\desk\python\python\lizi\pdf2img.py", line 2, in <module>
import fitz
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\fitz\__init__.py", line 1, in <module>
from frontend import *
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\frontend\__init__.py", line 1, in <module>
from .events import *
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\frontend\events\__init__.py", line 1, in <module>
from .clipboard import *
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\frontend\events\clipboard.py", line 2, in <module>
from ..dom import Event
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\frontend\dom.py", line 439, in <module>
from . import dispatcher
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\frontend\dispatcher.py", line 15, in <module>
from . import config, server
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\frontend\server.py", line 24, in <module>
app.mount(config.STATIC_ROUTE, StaticFiles(directory=config.STATIC_DIRECTORY), name=config.STATIC_NAME)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\starlette\staticfiles.py", line 55, in __init__
raise RuntimeError(f"Directory '{directory}' does not exist")
RuntimeError: Directory 'static/' does not exist 谢谢楼主分享。 感谢原创分享!感谢有您! 刚学会用Python,就看见实用教程,赞! 感谢分享,学习一下 不错,在pdf很多的时候,很实用 试试看。。。 有用,就是个别注释不准确。。。 不错 可以植入到小工具里面