当前位置:网站首页>PDF转图片实现方式
PDF转图片实现方式
2022-06-22 06:57:00 【&永恒的星河&】
1. 需要python包
PyPDF2
glob
pdf2image
numpy
2. PyPDF2转图片步骤
- 使用PdfFileWriter类可以用来生成一个新的PDF对象pdf_writer。
- PdfFileReader是PyPDF2包的一个类,拥有与PDF文档交互的多种方法,调用了该类创造了一个可用来读取的对象,这个对象的名称为pdf 。
- 调用pdf对象的方法getPage()方法去取得就PDF文件的目标页,下面代码中我用来获取PDF第一页,也就是此方法的参数为整数零,此方法返回一个页面对象,名称为page。
- 调用pdf_writer.addPage(page)将步骤3中page对象加入pdf_writer列表。
- 调用pdf_writer.write(outfile)将pdf_writer列表中指定页面保存到临时文件中outfile。
- 调用pdf2image中convert_from_bytes方法将5中临时文件中outfile生成目标图片。
3. 需要按照依赖poppler
Poppler是用于呈现可移植文档格式(PDF)文档的免费软件实用程序库。不同机器安装poppler方式如下:
3.1 mac机器
- Press
Command+Spaceand type Terminal and press enter/return key.- 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.- Now, copy/paste and run this command to make brew command available inside the Terminal:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile- Copy and paste the following command:
brew install popplerDone! You can now use
poppler.
3.2 window安装
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()
边栏推荐
- Py之scorecardpy:scorecardpy的简介、安装、使用方法之详细攻略
- 生成字符串方式
- [openairinterface5g] ITTI messaging mechanism
- SQL injection vulnerability (x) secondary injection
- OpenGL - Textures
- 【GCN-RS】UltraGCN: Ultra Simplification of Graph Convolutional Networks for Recommendation (CIKM‘21)
- 【M32】单片机 xxx.map 文件简单解读
- 圣杯布局和双飞翼布局的区别
- The journey of an operator in the framework of deep learning
- Data security practice guide - data collection security management
猜你喜欢

Golang calls sdl2, plays PCM audio, and reports an error signal arrived during external code execution.

6. 安装ssh连接工具(用于我们连接实验室的服务器)

Advanced usage of setting breakpoints during keil debugging

代理模式与装饰模式到底哪家强

How to learn 32-bit MCU
![[php] composer installation](/img/37/7adaca01b95085b42a116bc6b08165.png)
[php] composer installation

Chrome install driver

Introduction to 51 Single Chip Microcomputer -- the use of Proteus 8 professional
![[php]tp6 cli mode to create tp6 and multi application configurations and common problems](/img/19/0a3319b04fe6449c90ade6f27fca4a.png)
[php]tp6 cli mode to create tp6 and multi application configurations and common problems

Databricks from open source to commercialization
随机推荐
golang调用sdl2,播放pcm音频,报错signal arrived during external code execution。
OpenGL - Draw Triangle
Leetcode--- search insertion location
【M32】单片机 xxx.map 文件简单解读
编程题:移除数组中的元素(JS实现)
仙人掌之歌——进军To C直播(2)
【GAN】W-GAN ICLR‘17, ICML‘17
Introduction notes to quantum computing (continuously updated)
[tp6] using the workman websocket
Difference between grail layout and twin wing layout
[M32] single chip microcomputer SVN setting ignore file
Data security practice guide - data collection security practice - data classification and classification
Databricks from open source to commercialization
OpenGL - Textures
[M32] SCM xxx Simple interpretation of map file
Cesium loading 3D tiles model
Advanced usage of setting breakpoints during keil debugging
Xh_ CMS penetration test documentation
PIP for source changing and accelerated downloading
迪进面向ConnectCore系统模块推出Digi ConnectCore语音控制软件