Spaces:
Sleeping
Sleeping
feat: PDF→CSV agent v1.0.0
Browse files- core/extractor.py : extract_pages() via PyMuPDF
- core/agent.py : extract_articles() via Claude Sonnet (strict JSON)
- core/csv_writer.py : to_csv() with PV HT calculation and pandas
- app.py : Gradio UI (upload, page range, margin, unit)
- docs/index.html : GitHub Pages landing page
- requirements.txt, .gitignore, .env.example
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- .env.example +1 -0
- .gitignore +13 -0
- README.md +61 -1
- app.py +71 -0
- core/__init__.py +0 -0
- core/agent.py +36 -0
- core/csv_writer.py +19 -0
- core/extractor.py +9 -0
- docs/index.html +88 -0
- requirements.txt +5 -0
.env.example
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ANTHROPIC_API_KEY=your_api_key_here
|
.gitignore
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*.pyo
|
| 5 |
+
.venv/
|
| 6 |
+
venv/
|
| 7 |
+
*.egg-info/
|
| 8 |
+
dist/
|
| 9 |
+
build/
|
| 10 |
+
.DS_Store
|
| 11 |
+
Thumbs.db
|
| 12 |
+
*.csv
|
| 13 |
+
!tests/fixtures/*.csv
|
README.md
CHANGED
|
@@ -1 +1,61 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PDF → CSV Agent
|
| 2 |
+
|
| 3 |
+
Extrait les articles d'un catalogue PDF et génère un fichier CSV tarifaire prêt à l'import.
|
| 4 |
+
|
| 5 |
+
## Fonctionnement
|
| 6 |
+
|
| 7 |
+
1. Chargez un PDF de catalogue fournisseur
|
| 8 |
+
2. Sélectionnez la plage de pages à analyser
|
| 9 |
+
3. Renseignez votre taux de marge et l'unité par défaut
|
| 10 |
+
4. Cliquez **Extraire → CSV** — l'IA (Claude Sonnet) analyse le texte et produit le CSV
|
| 11 |
+
|
| 12 |
+
Le CSV contient : `Code article`, `Libellé`, `PV HT` (calculé depuis le prix TTC et la marge), `Unité`.
|
| 13 |
+
|
| 14 |
+
## Installation
|
| 15 |
+
|
| 16 |
+
```bash
|
| 17 |
+
python -m venv .venv
|
| 18 |
+
# Windows
|
| 19 |
+
.venv\Scripts\activate
|
| 20 |
+
# macOS / Linux
|
| 21 |
+
source .venv/bin/activate
|
| 22 |
+
|
| 23 |
+
pip install -r requirements.txt
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
Créez un fichier `.env` à partir de `.env.example` et renseignez votre clé API Anthropic :
|
| 27 |
+
|
| 28 |
+
```
|
| 29 |
+
ANTHROPIC_API_KEY=sk-ant-...
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
## Lancement
|
| 33 |
+
|
| 34 |
+
```bash
|
| 35 |
+
python app.py
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
L'interface Gradio s'ouvre sur `http://localhost:7860`.
|
| 39 |
+
|
| 40 |
+
## Structure
|
| 41 |
+
|
| 42 |
+
```
|
| 43 |
+
core/
|
| 44 |
+
extractor.py # extraction texte PDF (PyMuPDF)
|
| 45 |
+
agent.py # appel Claude Sonnet → JSON articles
|
| 46 |
+
csv_writer.py # calcul PV HT + export CSV (pandas)
|
| 47 |
+
app.py # interface Gradio
|
| 48 |
+
docs/ # GitHub Pages landing
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
## Formule de calcul
|
| 52 |
+
|
| 53 |
+
```
|
| 54 |
+
PV HT = Prix TTC / (1 + marge%)
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
Exemple avec marge 30 % : `100 € TTC → 76,92 € HT`
|
| 58 |
+
|
| 59 |
+
## Licence
|
| 60 |
+
|
| 61 |
+
MIT
|
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
from core.extractor import extract_pages
|
| 7 |
+
from core.agent import extract_articles
|
| 8 |
+
from core.csv_writer import to_csv
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def process(pdf_file, page_from, page_to, margin_pct, default_unit):
|
| 12 |
+
if pdf_file is None:
|
| 13 |
+
return None, "Veuillez charger un fichier PDF."
|
| 14 |
+
|
| 15 |
+
p_from, p_to = int(page_from), int(page_to)
|
| 16 |
+
if p_from > p_to:
|
| 17 |
+
return None, "La page de début doit être ≤ à la page de fin."
|
| 18 |
+
|
| 19 |
+
pages = extract_pages(pdf_file, p_from, p_to)
|
| 20 |
+
if not pages:
|
| 21 |
+
return None, "Aucune page extraite dans la plage indiquée."
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
articles = extract_articles(pages, float(margin_pct), default_unit)
|
| 25 |
+
except Exception as exc:
|
| 26 |
+
return None, f"Erreur lors de l'extraction IA : {exc}"
|
| 27 |
+
|
| 28 |
+
if not articles:
|
| 29 |
+
return None, "Aucun article détecté dans ces pages."
|
| 30 |
+
|
| 31 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".csv", delete=False)
|
| 32 |
+
tmp.close()
|
| 33 |
+
to_csv(articles, tmp.name, float(margin_pct))
|
| 34 |
+
return tmp.name, f"{len(articles)} articles extraits."
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
with gr.Blocks(title="PDF → CSV", theme=gr.themes.Soft()) as demo:
|
| 38 |
+
gr.Markdown(
|
| 39 |
+
"## PDF → CSV\n"
|
| 40 |
+
"Extrait les articles d'un catalogue PDF et génère un fichier CSV tarifaire prêt à l'import."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
pdf_input = gr.File(label="Fichier PDF", file_types=[".pdf"], type="filepath")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
page_from = gr.Slider(minimum=1, maximum=500, value=1, step=1, label="Page de")
|
| 48 |
+
page_to = gr.Slider(minimum=1, maximum=500, value=10, step=1, label="Page à")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
margin_pct = gr.Number(value=30, minimum=0, maximum=100, label="Marge %")
|
| 52 |
+
default_unit = gr.Dropdown(
|
| 53 |
+
choices=["ML", "M²", "Pièce", "Rouleau"],
|
| 54 |
+
value="Pièce",
|
| 55 |
+
label="Unité par défaut",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
run_btn = gr.Button("Extraire → CSV", variant="primary")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
csv_output = gr.File(label="Télécharger le CSV")
|
| 62 |
+
status_msg = gr.Textbox(label="Statut", interactive=False)
|
| 63 |
+
|
| 64 |
+
run_btn.click(
|
| 65 |
+
fn=process,
|
| 66 |
+
inputs=[pdf_input, page_from, page_to, margin_pct, default_unit],
|
| 67 |
+
outputs=[csv_output, status_msg],
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|
core/__init__.py
ADDED
|
File without changes
|
core/agent.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import anthropic
|
| 3 |
+
|
| 4 |
+
_SYSTEM = """\
|
| 5 |
+
Tu es un extracteur d'articles de catalogue. À partir du texte de pages PDF, \
|
| 6 |
+
extrais chaque article et retourne UNIQUEMENT un tableau JSON valide.
|
| 7 |
+
Chaque objet doit avoir exactement ces quatre champs :
|
| 8 |
+
- "code" : string — référence/code article, chaîne vide si absent
|
| 9 |
+
- "libelle" : string — désignation complète de l'article
|
| 10 |
+
- "prix_ttc" : number — prix TTC en euros (0 si absent ou non trouvé)
|
| 11 |
+
- "unite" : string — unité de vente ; utilise l'unité par défaut fournie si non précisée
|
| 12 |
+
Ne retourne aucun texte, commentaire ou balise avant ou après le tableau JSON.\
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def extract_articles(
|
| 17 |
+
pages_text: list[str],
|
| 18 |
+
margin_pct: float,
|
| 19 |
+
default_unit: str,
|
| 20 |
+
) -> list[dict]:
|
| 21 |
+
"""Call Claude to parse catalogue pages and return a list of article dicts."""
|
| 22 |
+
client = anthropic.Anthropic()
|
| 23 |
+
combined = "\n\n--- PAGE ---\n\n".join(pages_text)
|
| 24 |
+
user_msg = f"Unité par défaut : {default_unit}\n\nTexte du catalogue :\n{combined}"
|
| 25 |
+
|
| 26 |
+
message = client.messages.create(
|
| 27 |
+
model="claude-sonnet-4-6",
|
| 28 |
+
max_tokens=8096,
|
| 29 |
+
system=_SYSTEM,
|
| 30 |
+
messages=[{"role": "user", "content": user_msg}],
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
raw = message.content[0].text.strip()
|
| 34 |
+
if raw.startswith("```"):
|
| 35 |
+
raw = raw.split("\n", 1)[1].rsplit("```", 1)[0]
|
| 36 |
+
return json.loads(raw)
|
core/csv_writer.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def to_csv(articles: list[dict], output_path: str, margin_pct: float = 30.0) -> str:
|
| 5 |
+
"""Write articles to a semicolon-separated CSV and return the path."""
|
| 6 |
+
margin = margin_pct / 100
|
| 7 |
+
rows = []
|
| 8 |
+
for a in articles:
|
| 9 |
+
prix_ttc = float(a.get("prix_ttc") or 0)
|
| 10 |
+
pv_ht = round(prix_ttc / (1 + margin), 2) if prix_ttc else 0.0
|
| 11 |
+
rows.append({
|
| 12 |
+
"Code article": a.get("code", ""),
|
| 13 |
+
"Libellé": a.get("libelle", ""),
|
| 14 |
+
"PV HT": pv_ht,
|
| 15 |
+
"Unité": a.get("unite", ""),
|
| 16 |
+
})
|
| 17 |
+
df = pd.DataFrame(rows, columns=["Code article", "Libellé", "PV HT", "Unité"])
|
| 18 |
+
df.to_csv(output_path, index=False, sep=";", encoding="utf-8-sig")
|
| 19 |
+
return output_path
|
core/extractor.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def extract_pages(pdf_path: str, page_from: int, page_to: int) -> list[str]:
|
| 5 |
+
"""Return one string per PDF page for the given 1-based page range."""
|
| 6 |
+
doc = fitz.open(pdf_path)
|
| 7 |
+
start = max(0, page_from - 1)
|
| 8 |
+
end = min(doc.page_count, page_to)
|
| 9 |
+
return [doc[i].get_text() for i in range(start, end)]
|
docs/index.html
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="fr">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>PDF → CSV Agent</title>
|
| 7 |
+
<style>
|
| 8 |
+
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
| 9 |
+
body {
|
| 10 |
+
font-family: system-ui, -apple-system, sans-serif;
|
| 11 |
+
background: #f8f9fa;
|
| 12 |
+
color: #212529;
|
| 13 |
+
display: flex;
|
| 14 |
+
flex-direction: column;
|
| 15 |
+
align-items: center;
|
| 16 |
+
min-height: 100vh;
|
| 17 |
+
padding: 3rem 1rem;
|
| 18 |
+
}
|
| 19 |
+
.card {
|
| 20 |
+
background: #fff;
|
| 21 |
+
border-radius: 12px;
|
| 22 |
+
box-shadow: 0 2px 16px rgba(0,0,0,.08);
|
| 23 |
+
max-width: 680px;
|
| 24 |
+
width: 100%;
|
| 25 |
+
padding: 2.5rem 2rem;
|
| 26 |
+
}
|
| 27 |
+
h1 { font-size: 2rem; margin-bottom: .5rem; }
|
| 28 |
+
.badge {
|
| 29 |
+
display: inline-block;
|
| 30 |
+
background: #0d6efd;
|
| 31 |
+
color: #fff;
|
| 32 |
+
font-size: .75rem;
|
| 33 |
+
font-weight: 600;
|
| 34 |
+
padding: .2rem .6rem;
|
| 35 |
+
border-radius: 999px;
|
| 36 |
+
text-decoration: none;
|
| 37 |
+
margin-bottom: 1.5rem;
|
| 38 |
+
}
|
| 39 |
+
p { line-height: 1.7; color: #495057; margin-bottom: 1rem; }
|
| 40 |
+
ul { padding-left: 1.4rem; color: #495057; line-height: 1.9; margin-bottom: 1.5rem; }
|
| 41 |
+
.actions { display: flex; gap: 1rem; flex-wrap: wrap; margin-top: 1.5rem; }
|
| 42 |
+
.btn {
|
| 43 |
+
display: inline-block;
|
| 44 |
+
padding: .6rem 1.2rem;
|
| 45 |
+
border-radius: 8px;
|
| 46 |
+
text-decoration: none;
|
| 47 |
+
font-weight: 600;
|
| 48 |
+
font-size: .9rem;
|
| 49 |
+
transition: opacity .15s;
|
| 50 |
+
}
|
| 51 |
+
.btn:hover { opacity: .85; }
|
| 52 |
+
.btn-primary { background: #0d6efd; color: #fff; }
|
| 53 |
+
.btn-secondary { background: #e9ecef; color: #212529; }
|
| 54 |
+
footer { margin-top: 2.5rem; font-size: .8rem; color: #adb5bd; }
|
| 55 |
+
</style>
|
| 56 |
+
</head>
|
| 57 |
+
<body>
|
| 58 |
+
<div class="card">
|
| 59 |
+
<h1>PDF → CSV Agent</h1>
|
| 60 |
+
<a class="badge" href="https://github.com/boutique-oss/convertionpdfcsv/releases">v1.0.0</a>
|
| 61 |
+
|
| 62 |
+
<p>
|
| 63 |
+
Outil open-source qui extrait automatiquement les articles d'un catalogue PDF
|
| 64 |
+
(code, libellé, prix TTC) et génère un fichier CSV tarifaire prêt à l'import,
|
| 65 |
+
avec calcul du prix de vente HT selon votre taux de marge.
|
| 66 |
+
</p>
|
| 67 |
+
|
| 68 |
+
<p><strong>Fonctionnalités :</strong></p>
|
| 69 |
+
<ul>
|
| 70 |
+
<li>Extraction de texte page par page via PyMuPDF</li>
|
| 71 |
+
<li>Analyse IA (Claude Sonnet) avec sortie JSON stricte</li>
|
| 72 |
+
<li>Calcul automatique du PV HT depuis le prix TTC et la marge</li>
|
| 73 |
+
<li>Interface Gradio — aucun code requis</li>
|
| 74 |
+
<li>CSV encodé UTF-8 BOM, séparateur point-virgule (compatible Excel)</li>
|
| 75 |
+
</ul>
|
| 76 |
+
|
| 77 |
+
<div class="actions">
|
| 78 |
+
<a class="btn btn-primary" href="https://github.com/boutique-oss/convertionpdfcsv#readme">
|
| 79 |
+
Documentation (README)
|
| 80 |
+
</a>
|
| 81 |
+
<a class="btn btn-secondary" href="https://github.com/boutique-oss/convertionpdfcsv">
|
| 82 |
+
Code source
|
| 83 |
+
</a>
|
| 84 |
+
</div>
|
| 85 |
+
</div>
|
| 86 |
+
<footer>MIT License — boutique-oss</footer>
|
| 87 |
+
</body>
|
| 88 |
+
</html>
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
anthropic>=0.40.0
|
| 2 |
+
gradio>=4.0.0
|
| 3 |
+
pandas>=2.0.0
|
| 4 |
+
PyMuPDF>=1.24.0
|
| 5 |
+
python-dotenv>=1.0.0
|