惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
雷峰网
雷峰网
博客园 - 叶小钗
V
V2EX
博客园 - Franky
博客园_首页
小众软件
小众软件
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
S
SegmentFault 最新的问题
B
Blog RSS Feed
博客园 - 【当耐特】
D
Docker
N
Netflix TechBlog - Medium

云言博客

蓝月亮SAM系统签名算法(sign)生成逆向-云言博客 2025-我的年中总结-云言博客 finalshell 4.5 离线码计算-云言博客 2024-我的年度总结-云言博客 彩虹聚合DNS管理系统v1.7更新-云言博客 [Windows] PC 微信防撤回插件 适用3.9.11.25【2024-8-25更新】-云言博客 使用Python查询任意地区历史天气并生成气温走势折线图-云言博客 CoreNext主题1.6.6 全开源免授权版-云言博客 PHP将API接口获取的数据保存到本地服务器-云言博客
PDF逐页转换成PNG,并且自动排序-云言博客
152113081031W+ · 2024-11-01 · via 云言博客

前两天看到有人发pdf转图片的软件,正好自己也在学python,就用Python写了一个,它可以自动检测当前py文件目录下的pdf文件,然后逐页转换成png格式,并存储到目录下的“转换后的文件夹”,文件名以001,002,003…命名;

图片[1]-PDF逐页转换成PNG,并且自动排序-云言博客

注意:pdf2image库需要安装Poppler,安装后需要重启代码编辑器
目录和名字之类的大家可以修改成自己需要的即可~

import os
from PyPDF2 import PdfReader
from pdf2image import convert_from_path
 
def convert_pdf_to_images(pdf_path, output_folder):
    # 创建输出文件夹如果不存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
 
    # 打开PDF文件
    with open(pdf_path, 'rb') as file:
        reader = PdfReader(file)
        num_pages = len(reader.pages)
 
        # 遍历每一页
        for page_number in range(num_pages):
            # 将PDF页面转换为图像
            images = convert_from_path(pdf_path, first_page=page_number + 1, last_page=page_number + 1)
            image = images[0]
            # 保存图像
            image_filename = f"{output_folder}/{page_number + 1:03d}.png"
            image.save(image_filename, "PNG")
            print(f"Saved {image_filename}")
 
# 获取当前目录下所有PDF文件
pdf_files = [f for f in os.listdir('.') if f.endswith('.pdf')]
 
# 转换每个PDF文件
for pdf_file in pdf_files:
    convert_pdf_to_images(pdf_file, '转换后')