IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> PDF转图片实现方式 -> 正文阅读

[Python知识库]PDF转图片实现方式

1. 需要python包

PyPDF2

glob

pdf2image

numpy

2.?PyPDF2转图片步骤

  1. 使用PdfFileWriter类可以用来生成一个新的PDF对象pdf_writer。
  2. PdfFileReader是PyPDF2包的一个类,拥有与PDF文档交互的多种方法,调用了该类创造了一个可用来读取的对象,这个对象的名称为pdf?。
  3. 调用pdf对象的方法getPage()方法去取得就PDF文件的目标页,下面代码中我用来获取PDF第一页,也就是此方法的参数为整数零,此方法返回一个页面对象,名称为page。
  4. 调用pdf_writer.addPage(page)将步骤3中page对象加入pdf_writer列表。
  5. 调用pdf_writer.write(outfile)将pdf_writer列表中指定页面保存到临时文件中outfile。
  6. 调用pdf2image中convert_from_bytes方法将5中临时文件中outfile生成目标图片。

3. 需要按照依赖poppler

Poppler是用于呈现可移植文档格式(PDF)文档的免费软件实用程序库。不同机器安装poppler方式如下:

3.1 mac机器

  1. Press?Command+Space?and type?Terminal?and press?enter/return?key.
  2. Copy and paste the following command in Terminal app:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    and press?enter/return?key. Wait for the command to finish. If you are prompted to enter a password, please type your Mac user's login password and press ENTER. Mind you, as you type your password, it won't be visible on your Terminal (for security reasons), but rest assured it will work.
  3. Now, copy/paste and run this command to make?brew?command available inside the Terminal:?echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
  4. Copy and paste the following command:
    brew install poppler

Done! You can now use?poppler.

3.2? window安装

Poppler for Windows

3.3 linux安装

apt-get install python-poppler?

或者

sudo apt-get install python3-poppler-qt4

4. Python实现转图片代码

from PyPDF2 import PdfFileReader, PdfFileWriter
import glob
import os
from pdf2image import convert_from_bytes
import shutil
import numpy as np
from time import time

def pdf2image2(pdfPath, imagePath):
    images = convert_from_bytes(open(pdfPath, 'rb').read())

    for image in images:
        if not os.path.exists(imagePath):
            os.makedirs(imagePath)
        pngname=pdfPath[6:-4]
        image.save(imagePath+'/'+pngname+'.jpg', 'JPEG',quality=30)

def process_bar(no, total_length):
    bar = '\r' + str(no) + '|' + str(total_length)
    print(bar, end='', flush=True)

def split_combine(path, pdf_writer):
    pdf = PdfFileReader(path, strict=False)
    # lastest page
    page = pdf.getPage(0)
    pdf_writer.addPage(page)

def run():
    # get curren dir pdf files
    start_time = time()
    pdf_list = glob.glob('*.pdf')
    pdf_writer = PdfFileWriter()
    imgpath="./img/"
    tmppath="./tmp/"
    if not os.path.exists(imgpath):
        os.makedirs(imgpath)
    if not os.path.exists(tmppath):
        os.makedirs(tmppath)
    for i, pdf_file in enumerate(pdf_list):
        process_bar(i + 1, len(pdf_list))
        split_combine(pdf_file, pdf_writer)
        with open(tmppath+pdf_file, 'wb') as output_pdf:
            pdf_writer.write(output_pdf)
        pdf2image2(tmppath+pdf_file, imgpath)
    shutil.rmtree(tmppath)
    end_time = time()
    print(end_time-start_time)


if __name__ == '__main__':
    run()
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-06-23 00:52:48  更:2022-06-23 00:52:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 12:57:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码