Python实现PDF转图片

Python实现PDF转图片

  • 使用convert_from_path将PDF转为图片

使用convert_from_path将PDF转为图片

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib
import os

from pathlib import Path
from pdf2image import convert_from_path

BASE_DIR = Path(__file__).resolve().parent.parent
# 缓存地址
CACHE_URL = os.path.join(BASE_DIR, 'cache')
# Poppler组件地址
POPPLER_PATH = os.path.join(BASE_DIR, 'poppler-0.68.0', 'bin')


class Pdf:

    source = ''

    def __init__(self, source):
        self.source = source

    def to_image(self):
        """
        将pdf转换为png后,保存在dirname文件夹.
        """
        image_path = os.path.join(CACHE_URL, hashlib.md5(self.source.encode('gb2312')).hexdigest())
        if not os.path.exists(CACHE_URL):
            os.mkdir(CACHE_URL)
        if not os.path.exists(image_path):
            os.mkdir(image_path)
            convert_from_path(pdf_path=self.source,
                              fmt='png',
                              output_folder=image_path,
                              poppler_path=POPPLER_PATH)
        file_names = os.listdir(image_path)
        file_paths = []
        if len(file_names) > 0:
            for i in range(len(file_names)):
                file_paths.append(image_path + '\\' + file_names[i])
        return file_paths

你可能感兴趣的:(Python,PDF,1024程序员节)