python批量转化pdf图片为jpg图片

1.把pdf图片批量转为jpg;需要注意的是,需要先安装poppler这个软件,具体安装教程放在下面代码中了

2.代码

python 复制代码
#poppler安装教程参考:https://blog.csdn.net/wy01415/article/details/110257130
#windows上poppler下载链接:https://github.com/oschwartz10612/poppler-windows
from pdf2image import convert_from_path
from PIL import Image
import os


def convert_pdf_to_jpg(pdf_folder, output_folder, poppler_path):
    for pdf_file in os.listdir(pdf_folder):
        if pdf_file.endswith('.pdf'):
            pdf_path = os.path.join(pdf_folder, pdf_file)
            images = convert_from_path(pdf_path, poppler_path=poppler_path)

            for i, image in enumerate(images):
                #如果pdf有多页用下面这个代码
                # output_filename = f"{os.path.splitext(pdf_file)[0]}_page_{i + 1}.jpg"
                # 如果pdf就1页用下面这个代码
                output_filename = f"{os.path.splitext(pdf_file)[0]}.jpg"
                output_path = os.path.join(output_folder, output_filename)

                image.save(output_path, 'JPEG')


pdf_folder = 'E:/pythonworking/file/pdf_merge/workspace'
output_folder = 'E:/pythonworking/file/pdf_merge/workspace'
poppler_path = 'D:/software/Poppler/Release-24.02.0-0/poppler-24.02.0/Library/bin'

convert_pdf_to_jpg(pdf_folder, output_folder, poppler_path)

3.输出结果:

相关推荐
xinxiyinhe1 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
诸神缄默不语2 小时前
如何用Python 3自动打开exe程序
python·os·subprocess·python 3
橘子师兄2 小时前
分页功能组件开发
数据库·python·django
Logintern092 小时前
使用VS Code进行Python编程的一些快捷方式
开发语言·python
Multiple-ji3 小时前
想学python进来看看把
开发语言·python
liuyuzhongcc3 小时前
List 接口中的 sort 和 forEach 方法
java·数据结构·python·list
鸟哥大大3 小时前
【Python】pypinyin-汉字拼音转换工具
python·自然语言处理
jiugie3 小时前
MongoDB学习
数据库·python·mongodb
菜鸟单飞4 小时前
介绍一款非常实用的PDF阅读软件!
windows·pdf·电脑
阿尔法波4 小时前
python与pycharm如何设置文件夹为源代码根目录
开发语言·python·pycharm