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

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point 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, '转换后')