Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
tricount-scanner/receipt_parser.py at main · Neyroe/tricount-scanner · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Neyroe
tricount-scanner
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
tricount-scanner
/
receipt_parser.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
238 lines (196 loc) · 7.34 KB
main
Breadcrumbs
tricount-scanner
/
receipt_parser.py
Copy path
Top
File metadata and controls
Code
Blame
238 lines (196 loc) · 7.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""
Parsing de tickets de caisse exportés depuis les applis des enseignes.
Objectif : zéro IA, 100 % Python. Les applis (Mon E.Leclerc, etc.) exportent
des PDF avec une couche texte propre — on lit ce texte et on extrait les lignes.
Point d'entrée : parse_receipt(raw: bytes, filename: str) -> dict
Retourne {items: [{category, name, price}], ticket_store, ticket_date, total, warning?}
"""
import
io
import
csv
import
re
from
typing
import
Optional
# ---------------------------------------------------------------------------
# CSV (format E.Leclerc "Export Ticket de Caisse")
# ---------------------------------------------------------------------------
def
parse_csv
(
raw
:
bytes
)
->
dict
:
text
=
_decode
(
raw
)
reader
=
csv
.
reader
(
io
.
StringIO
(
text
))
items
=
[]
ticket_date
=
""
ticket_store
=
""
SKIP_STARTS
=
(
"rayon"
,
"export"
,
"date"
,
"total"
,
"bon"
,
"reste"
,
"sous"
)
for
row
in
reader
:
if
len
(
row
)
>=
1
:
cell
=
row
[
0
].
strip
()
if
cell
.
lower
().
startswith
(
"export ticket"
):
ticket_store
=
cell
.
replace
(
"Export Ticket de Caisse - "
,
""
).
strip
()
if
cell
.
lower
().
startswith
(
"date du ticket"
):
ticket_date
=
cell
.
split
(
":"
,
1
)[
-
1
].
strip
()
if
len
(
row
)
<
5
:
continue
rayon
=
row
[
0
].
strip
()
article
=
row
[
1
].
strip
()
total
=
row
[
4
].
strip
().
replace
(
","
,
"."
)
if
not
rayon
or
not
article
:
continue
if
any
(
rayon
.
lower
().
startswith
(
s
)
for
s
in
SKIP_STARTS
):
continue
if
any
(
article
.
lower
().
startswith
(
s
)
for
s
in
SKIP_STARTS
):
continue
try
:
price
=
float
(
total
)
except
ValueError
:
continue
if
price
<=
0
:
continue
items
.
append
({
"category"
:
rayon
.
strip
(),
"name"
:
article
.
strip
(),
"price"
:
price
})
return
{
"items"
:
items
,
"ticket_store"
:
ticket_store
,
"ticket_date"
:
ticket_date
,
"total"
:
round
(
sum
(
i
[
"price"
]
for
i
in
items
),
2
),
}
# ---------------------------------------------------------------------------
# PDF
# ---------------------------------------------------------------------------
def
parse_pdf
(
raw
:
bytes
)
->
dict
:
import
pdfplumber
# imported lazily so the app runs even if absent
pages_text
=
[]
with
pdfplumber
.
open
(
io
.
BytesIO
(
raw
))
as
pdf
:
for
page
in
pdf
.
pages
:
pages_text
.
append
(
page
.
extract_text
()
or
""
)
text
=
"
\n
"
.
join
(
pages_text
)
if
not
text
.
strip
():
raise
ValueError
(
"Ce PDF ne contient pas de texte lisible (probablement une image scannée). "
"Exportez un PDF depuis l'appli de l'enseigne."
)
store
=
_detect_store
(
text
)
if
store
==
"leclerc"
:
return
_parse_leclerc
(
text
)
return
_parse_generic
(
text
)
def
_detect_store
(
text
:
str
)
->
Optional
[
str
]:
low
=
text
.
lower
()
if
"leclerc"
in
low
:
return
"leclerc"
if
"lidl"
in
low
:
return
"lidl"
if
"carrefour"
in
low
:
return
"carrefour"
if
"auchan"
in
low
:
return
"auchan"
return
None
# E.Leclerc: lignes "[* ]NOM PRIX CODE_TVA" entre l'en-tête "TTC TVA" et "Total N articles"
_LECLERC_ITEM
=
re
.
compile
(
r"^(?:\*\s+)?(.+?)\s+(\d+[.,]\d{2})\s+(\d)$"
)
_LECLERC_TOTAL
=
re
.
compile
(
r"Total\s+\d+\s+articles?\s+(\d+[.,]\d{2})"
,
re
.
IGNORECASE
)
# code TVA E.Leclerc -> libellé de rayon approximatif (1 = alimentaire 5.5%, 3 = 20%)
_LECLERC_TVA_CAT
=
{
"1"
:
"Alimentaire"
,
"3"
:
"Autres"
}
def
_parse_leclerc
(
text
:
str
)
->
dict
:
lines
=
text
.
split
(
"
\n
"
)
items
=
[]
in_items
=
False
for
ln
in
lines
:
s
=
ln
.
strip
()
if
re
.
search
(
r"\bTTC\b.*\bTVA\b"
,
s
):
in_items
=
True
continue
if
not
in_items
:
continue
if
s
.
startswith
(
"---"
)
or
s
.
lower
().
startswith
(
"total"
)
or
s
.
startswith
(
"CB"
):
break
m
=
_LECLERC_ITEM
.
match
(
s
)
if
not
m
:
continue
name
=
m
.
group
(
1
).
strip
()
price
=
float
(
m
.
group
(
2
).
replace
(
","
,
"."
))
tva
=
m
.
group
(
3
)
if
price
<=
0
:
continue
items
.
append
({
"category"
:
_LECLERC_TVA_CAT
.
get
(
tva
,
"Divers"
),
"name"
:
name
,
"price"
:
price
,
})
date
=
""
dm
=
re
.
search
(
r"(\d{1,2}\s+\w+\s+\d{4}\s+\d{1,2}:\d{2})"
,
text
)
if
dm
:
date
=
dm
.
group
(
1
)
result
=
{
"items"
:
items
,
"ticket_store"
:
"E.Leclerc"
,
"ticket_date"
:
date
,
"total"
:
round
(
sum
(
i
[
"price"
]
for
i
in
items
),
2
),
}
# Cross-check against the printed total
tm
=
_LECLERC_TOTAL
.
search
(
text
)
if
tm
:
printed
=
float
(
tm
.
group
(
1
).
replace
(
","
,
"."
))
result
[
"printed_total"
]
=
printed
if
abs
(
printed
-
result
[
"total"
])
>
0.01
:
result
[
"warning"
]
=
(
f"Somme des articles (
{
result
[
'total'
]:.2f
}
€) "
f"≠ total du ticket (
{
printed
:.2f
}
€). Vérifiez les lignes."
)
return
result
# Generic best-effort: any "NAME .... 12,34" line, skipping obvious non-items.
_GENERIC_ITEM
=
re
.
compile
(
r"^(.+?)\s+(\d+[.,]\d{2})(?:\s*[A-Z€]?)?$"
)
_GENERIC_SKIP
=
re
.
compile
(
r"total|sous.?total|^cb$|carte|montant|tva|espece|rendu|monnaie|"
r"remise|reduction|rabais|^ht$|^ttc$|caisse|ticket|siret|^code|^total"
,
re
.
IGNORECASE
,
)
def
_parse_generic
(
text
:
str
)
->
dict
:
items
=
[]
for
ln
in
text
.
split
(
"
\n
"
):
s
=
ln
.
strip
()
if
not
s
or
_GENERIC_SKIP
.
search
(
s
):
continue
m
=
_GENERIC_ITEM
.
match
(
s
)
if
not
m
:
continue
name
=
m
.
group
(
1
).
strip
(
" .-"
)
try
:
price
=
float
(
m
.
group
(
2
).
replace
(
","
,
"."
))
except
ValueError
:
continue
# Filter noise: name must have a couple letters and a plausible price
if
price
<=
0
or
price
>
2000
or
len
(
re
.
sub
(
r"[^A-Za-zÀ-ÿ]"
,
""
,
name
))
<
2
:
continue
items
.
append
({
"category"
:
"Divers"
,
"name"
:
name
,
"price"
:
price
})
return
{
"items"
:
items
,
"ticket_store"
:
_detect_store
(
text
)
or
""
,
"ticket_date"
:
""
,
"total"
:
round
(
sum
(
i
[
"price"
]
for
i
in
items
),
2
),
"warning"
:
"Format non reconnu — extraction générique, vérifiez les lignes."
,
}
# ---------------------------------------------------------------------------
# Dispatch
# ---------------------------------------------------------------------------
_IMAGE_MAGIC
=
(
b"
\x89
PNG"
,
b"
\xff
\xd8
\xff
"
,
b"GIF8"
,
b"RIFF"
,
b"BM"
)
def
parse_receipt
(
raw
:
bytes
,
filename
:
str
)
->
dict
:
name
=
(
filename
or
""
).
lower
()
if
name
.
endswith
(
".pdf"
)
or
raw
[:
5
]
==
b"%PDF-"
:
return
parse_pdf
(
raw
)
is_image
=
(
name
.
endswith
((
".png"
,
".jpg"
,
".jpeg"
,
".webp"
,
".gif"
,
".bmp"
))
or
raw
[:
4
]
in
_IMAGE_MAGIC
or
raw
[:
2
]
==
b"BM"
)
if
is_image
:
try
:
import
receipt_ocr
except
ImportError
:
raise
ValueError
(
"L'OCR image n'est pas disponible (easyocr non installé). "
"Utilisez un export PDF ou CSV."
)
return
receipt_ocr
.
parse_image
(
raw
)
return
parse_csv
(
raw
)
def
_decode
(
raw
:
bytes
)
->
str
:
for
enc
in
(
"utf-8-sig"
,
"utf-8"
,
"latin-1"
,
"cp1252"
):
try
:
return
raw
.
decode
(
enc
)
except
UnicodeDecodeError
:
continue
raise
ValueError
(
"Encodage du fichier non reconnu"
)
You can’t perform that action at this time.