25 lines
740 B
Python
25 lines
740 B
Python
import pymupdf
|
|
import io
|
|
from PIL import Image
|
|
import pytesseract
|
|
|
|
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
|
|
|
|
def extract_text_and_pictures(pdf_datei):
|
|
doc = pymupdf.open(pdf_datei)
|
|
text = ""
|
|
for seite in doc:
|
|
text += seite.get_text()
|
|
for bild in seite.get_images(full=True):
|
|
xref = bild[0]
|
|
basis_bild = doc.extract_image(xref)
|
|
bild_daten = basis_bild["image"]
|
|
bild_objekt = Image.open(io.BytesIO(bild_daten))
|
|
bild_text = pytesseract.image_to_string(bild_objekt, lang="deu")
|
|
text += " " + bild_text
|
|
return text
|
|
|
|
pdf_datei = 'any.pdf'
|
|
text = extract_text_and_pictures(pdf_datei)
|
|
|
|
print(text) |