pdf_auralia/pdf_oralia/extract.py

102 lines
2.9 KiB
Python
Raw Normal View History

2022-09-27 12:48:41 +00:00
from pathlib import Path
2022-09-27 13:01:14 +00:00
2022-09-27 12:48:41 +00:00
import click
2022-09-27 13:01:14 +00:00
import numpy as np
import pandas as pd
import pdfplumber
2022-09-27 12:48:41 +00:00
def extract_situation_loc(table):
try:
df = pd.DataFrame(table[1:], columns=table[0])
except IndexError:
print(table)
rows = []
for i, row in df[df["Locataires"] == "Totaux"].iterrows():
above_row_loc = df.iloc[i - 1]["Locataires"]
up_row = pd.concat(
[
row,
parse_above_loc(above_row_loc),
]
)
rows.append(up_row)
df_cleaned = pd.concat(rows, axis=1).T
df_cleaned.drop(["Locataires", "", "Période"], axis=1, inplace=True)
return df_cleaned
def parse_above_loc(content):
row = {}
try:
app, loc = content.split("\n")
except ValueError:
row["lot"] = ""
row["type"] = ""
row["locataire"] = content
else:
app_ = app.split(" ")
row["lot"] = app_[1]
row["type"] = " ".join(app_[2:])
row["locataire"] = loc
return pd.Series(row)
def extract_charge(table):
df = (
pd.DataFrame(table[1:], columns=table[0])
.replace("", np.nan)
.dropna(subset=["Débits"])
)
drop_index = df[
2022-09-27 13:01:14 +00:00
df["RECAPITULATIF DES OPERATIONS"].str.contains("TOTAUX", case=False)
2022-09-27 14:01:09 +00:00
| df["RECAPITULATIF DES OPERATIONS"].str.contains("solde", case=False)
2022-09-27 12:48:41 +00:00
].index
df.drop(drop_index, inplace=True)
return df
charge_table_settings = {
"vertical_strategy": "lines",
"horizontal_strategy": "text",
}
def extract_from_pdf(pdf, charge_dest, location_dest):
2022-09-27 14:01:09 +00:00
"""Build charge_dest and location_dest xlsx file from pdf"""
2022-09-27 12:48:41 +00:00
loc_table = []
for page in pdf.pages:
situation_loc_line = [
l
for l in page.extract_text().split("\n")
if "SITUATION DES LOCATAIRES" in l
]
if situation_loc_line:
mois, annee = situation_loc_line[0].split(" ")[-2:]
if loc_table:
loc_table += page.extract_table()[1:]
else:
loc_table = page.extract_table()
if "HONORAIRES" in page.extract_text():
df_charge = extract_charge(page.extract_table(charge_table_settings))
df_charge.to_excel(charge_dest, sheet_name="Charges", index=False)
print(f"{charge_dest} saved")
df_loc = extract_situation_loc(loc_table)
df_loc = df_loc.assign(mois=mois, annee=annee)
df_loc.to_excel(location_dest, sheet_name="Location", index=False)
print(f"{location_dest} saved")
2022-09-27 14:01:09 +00:00
def extract_save(pdf_file, dest):
"""Extract charge and locataire for pdf_file and put xlsx file in dest"""
2022-09-27 12:48:41 +00:00
pdf_file = Path(pdf_file)
2022-09-27 14:01:09 +00:00
xls_charge = dest / f"{pdf_file.stem.replace(' ', '_')}_charge.xlsx"
xls_locataire = dest / f"{pdf_file.stem.replace(' ', '_')}_locataire.xlsx"
2022-09-27 12:48:41 +00:00
pdf = pdfplumber.open(pdf_file)
extract_from_pdf(pdf, xls_charge, xls_locataire)