python 两种提取pdf中图片的包

项目需求,提取pdf中图片,找半天的结果

1.fitz (我没找到元素定位的方法)

pip install pymupdf

import fitz

def extract_pic_from_pdf(pdf_path, pic_forder):
    # 加载pdf
    doc = fitz.open(pdf_path)
    # 使用正则表达式来查找图片
    checkIM = r"/Subtype(?= */Image)"
    lenXREF = doc._getXrefLength() 
    
    # 提取图片
    for i in range(1, lenXREF):
        if not re.search(checkIM, doc._getXrefString(i)):
            continue
        pix = fitz.Pixmap(doc, i)
        pix.writePNG(os.path.join(..., ...))

2.pdfminer (含元素定位,官网提取图片是个坑,大多数教程也是错的)

pip install pdfminer3k

from pdfminer.pdfparser import PDFParser,PDFDocument 
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LAParams, LTImage, LTTextBoxHorizontal, LTFigure

def

你可能感兴趣的:(python,数据处理,python,数据挖掘)