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

推荐订阅源

博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
B
Blog
小众软件
小众软件
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
L
LangChain Blog
WordPress大学
WordPress大学
罗磊的独立博客
GbyAI
GbyAI
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
美团技术团队
F
Fortinet All Blogs
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog

云言博客

蓝月亮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, '转换后')