pdf2image

How to install?

  • 下载Python的第三方库 pdf2image
    • if you not have it already with pip install pillow
  • 库中需要pdf阅读器poppler
    • Windows users will have to install poppler for Windows,then add the bin/ folder to PATH
    • MAC users will have to install poppler for Mac
    • Linux users will have to both tools pre-installed with Ubuntu 16.04+ and Archlinux.If it`s not

How to use?

import tempfile
from pdf2image import convert_from_path,convert_from_bytes

#以下三种方式都可以读取文件,第三种最好
image = convert_from_path('data/0.pdf')
image = convert_from_bytes(open('data/0.pdf', 'rb').read())
image = convert_from_bytes(pdf_file, dpi=200, output_folder=None, first_page=None, last_page=None, fmt='ppm')
with tempfile.TemporaryDirectory() as path:
    convert_from_path(pdf_path, dpi=200, output_folder=None, first_page=None, last_page=None, fmt='ppm')
    
convert_from_path(pdf_path, dpi=200, output_folder=None, first_page=None, last_page=None,
                      fmt='ppm', thread_count=1, userpw=None, use_cropbox=False, strict=False, 					transparent=False,output_file=str(uuid.uuid4()), poppler_path=None)

参数

pdf_path pdf文件的存储路径

image 代表PDF文档每页的PIL图像列表

thread_count :允许设置用于转换的线程数;

first_page :允许设置由pdftoppm处理的第一个页面;

last_page:允许设置最后一页由pdftoppm处理;

fmt:允许指定输出格式。目前支持的格式是jpg、png和ppm;

dpi: 产生图片的质量

output_folder: 把新生的图片保存在指定路径下

userpw: PDF的passsword

use_cropdox :Use cropbox instead of mediabox

strict:当一个语法错误抛出异常时,他唱将作为异常引发

transparent:作为透明背景输出,而不是白色背景

output_file:输出图片格式的名字

poppler_path:poppler的文件路径(二进制)

你可能感兴趣的:(pdf2image)