python自动化--提取pdf中的图片

import fitz
import re
import os
from addres_file import file_name
import time


def pdf2image1(path, pic_path, image_name):
    try:
        checkIM = r"/Subtype(?= */Image)"
        pdf = fitz.open(path)
        lenXREF = pdf.xref_length()
        count = 1
        for i in range(1, lenXREF):
            text = pdf.xref_object(i)
            isImage = re.search(checkIM, text)
            if not isImage:
                continue
            pix = fitz.Pixmap(pdf, i)
            # new_name = f"img_{count}.png"
            new_name = '%s_img_%d.png' % (image_name, count)
            pix.save(os.path.join(pic_path, new_name))
            count += 1
            pix = None
    except Exception as error:
        print(error)


if __name__ == '__main__':
    name_list = file_name()
    start = time.time()
    for name in name_list:
        image_name = name
        file_path = r'E:\Python\提取图片pdf\1过程审核系统(操作手册)1.pdf'# PDF 文件路径
        dir_path = r'E:\Python\提取图片pdf'# 存放图片的文件夹
        # os.makedirs(dir_path)  # 根据路径,创建对应路径下的文件夹
        pdf2image1(file_path, dir_path, image_name)
    end = time.time()
    print('task is over: %.2f' % (end - start))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容