当前位置:网站首页>PDF转图片实现方式

PDF转图片实现方式

2022-06-22 06:57:00 &永恒的星河&

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()
原网站

版权声明
本文为[&永恒的星河&]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44402973/article/details/125357469