pdfminer
!pip install pdfminer.six
!python /usr/local/bin/pdf2txt.py -o data.txt data.pdf
Python
from pdfminer.high_level import extract_text
text = extract_text("data.pdf")
pdfbox
#Modifications d'URL avec la dernière version
!wget https://www-eu.apache.org/dist/pdfbox/2.0.21/pdfbox-app-2.0.21.jar -O pdfbox-app.jar
#Conversion de texte
!java -jar pdfbox-app.jar ExtractText -sort -encoding UTF-8 data.pdf
#image
!java -jar pdfbox-app.jar PDFToImage -imageType png -dpi 300 data.pdf
Python
!pip install python-pdfbox
import pdfbox
p = pdfbox.PDFBox()
p.extract_text("data.pdf", sort=True)
poppler
!apt install poppler-utils poppler-data
#Conversion de texte
!pdftotext -layout data.pdf
!pdfinfo data.pdf
#réparation
!pdftocairo -pdf data.pdf data_repaired.pdf
tabula
!wget https://github.com/tabulapdf/tabula-java/releases/download/v1.0.4/tabula-1.0.4-jar-with-dependencies.jar -O tabula.jar
# lattice
!java -jar tabula.jar -o data.csv -p all -l data.pdf
# stream
!java -jar tabula.jar -o data.csv -p all -t data.pdf
Python
!pip install tabula-py
import pandas as pd
from tabula import read_pdf
dfs = read_pdf("data.pdf", pages="all", lattice=True)
dfs = read_pdf("data.pdf", pages="all", lattice=True, pandas_options={"header": None})
Camelot
!apt install python3-tk ghostscript
!pip install camelot-py[cv]
# !pip install camelot-py[plot]
# !camelot --help
!camelot -p all -o data.csv -f csv lattice data.pdf
!camelot -p all -o data.csv -f csv -strip ' .\n' -split lattice -scale 40 data.pdf
Python
import camelot
tables = camelot.read_pdf("data.pdf", pages="all", split_text=True, strip_text=" \n", line_scale=40)
pdfplumber
!pip install pdfplumber
!apt install libmagickwand-dev ghostscript
#Pour pouvoir convertir un PDF en image/etc/ImageMagick-6/policy.Écraser le XML
%%writefile /etc/ImageMagick-6/policy.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap>
<policymap>
<policy domain="resource" name="memory" value="256MiB"/>
<policy domain="resource" name="map" value="512MiB"/>
<policy domain="resource" name="width" value="16KP"/>
<policy domain="resource" name="height" value="16KP"/>
<policy domain="resource" name="area" value="128MB"/>
<policy domain="resource" name="disk" value="1GiB"/>
<policy domain="delegate" rights="none" pattern="URL"/>
<policy domain="delegate" rights="none" pattern="HTTPS"/>
<policy domain="delegate" rights="none" pattern="HTTP"/>
<policy domain="path" rights="none" pattern="@*"/>
<policy domain="cache" name="shared-secret" value="passphrase" stealth="true"/>
<policy domain="coder" rights="none" pattern="PS"/>
<policy domain="coder" rights="none" pattern="PS2"/>
<policy domain="coder" rights="none" pattern="PS3"/>
<policy domain="coder" rights="none" pattern="EPS"/>
<policy domain="coder" rights="read|write" pattern="PDF" />
<policy domain="coder" rights="none" pattern="XPS"/>
</policymap>
!pdfplumber < data.pdf > data.csv
import pdfplumber
import pandas as pd
pdf = pdfplumber.open("data.pdf")
page = pdf.pages[0]
page.find_tables()[0]
#Vérifiez la position des caractères
page.chars
#Obtenir du texte avec recadrage
crop = page.within_bbox((0, 90, p0.width, 105))
s = crop.extract_text()
s
#Confirmation PDF
im = page.to_image()
im
table_settings = {
#Référence verticale
"vertical_strategy": "lines",
#Spécifiez le délimiteur vertical numériquement (liste)
"explicit_vertical_lines": [],
#Référence horizontale
"horizontal_strategy": "lines",
#Spécifiez la division horizontale numériquement (liste)
"explicit_horizontal_lines": [],
#S'il se trouve dans la plage autorisée, ajustez à la même position horizontale ou verticale * Ajuster ci-dessus?
"snap_tolerance": 3,
#Si dans la tolérance, rejoignez
"join_tolerance": 3,
#Bords plus courts que détruits avant de tenter de reconstruire la table?
"edge_min_length": 3,
#Hauteur minimale des caractères
"min_words_vertical": 3,
#Hauteur minimale des caractères
"min_words_horizontal": 1,
#Reconnaître les caractères vides comme faisant partie d'un mot et ne pas les utiliser comme délimiteurs
"keep_blank_chars": False,
#Reconnu comme un mot si l'espacement des caractères est le suivant
"text_tolerance": 3,
"text_x_tolerance": None,
"text_y_tolerance": None,
#Tolérance si les bords gauche et droit du texte ne correspondent pas exactement aux lignes verticales?
"intersection_tolerance": 3,
"intersection_x_tolerance": None,
"intersection_y_tolerance": None,
}
#Confirmation du personnage
im.reset().draw_rects(page.extract_words())
#Consultez le tableau
im.reset().debug_tablefinder()
with pdfplumber.open("data.pdf") as pdf:
dfs = []
for page in pdf.pages:
table = page.extract_table(table_settings)
df_tmp = pd.DataFrame(table[1:], columns=table[0])
dfs.append(df_tmp)
df = pd.concat(dfs)
OCR
tesseract-ocr
!add-apt-repository ppa:alex-p/tesseract-ocr -y
!apt update
!apt install tesseract-ocr
!apt install libtesseract-dev
!tesseract -v
!apt install tesseract-ocr-jpn tesseract-ocr-jpn-vert
!apt install tesseract-ocr-script-jpan tesseract-ocr-script-jpan-vert
!tesseract --list-langs
!pip install pytesseract
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
img = cv2.imread("test.jpg ")
#Inversion noir et blanc
img_gray, _ = cv2.decolor(img)
cv2_imshow(img_gray)
Recommended Posts