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

推荐订阅源

T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Cisco Talos Blog
Cisco Talos Blog
AI
AI
L
LINUX DO - 最新话题
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The GitHub Blog
The GitHub Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
S
Securelist
博客园_首页
IT之家
IT之家
Schneier on Security
Schneier on Security
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
The Register - Security
The Register - Security
D
DataBreaches.Net
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Recorded Future
Recorded Future
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tailwind CSS Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com

云言博客

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