-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
549 lines (464 loc) · 23.3 KB
/
Copy pathmain.py
File metadata and controls
549 lines (464 loc) · 23.3 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import pdfplumber
import pandas as pd
import os
import re
import logging
from datetime import datetime
from openpyxl.utils import get_column_letter
# --- CONFIGURATION ---
INPUT_DIR = "fp"
OUTPUT_FILE = "invoice_summary.xlsx"
LOG_FILE = "extraction.log"
# Setup Logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(LOG_FILE),
logging.StreamHandler()
]
)
def clean_name(n):
"""
Cleans and validates company names.
Returns None if invalid.
"""
if not n: return None
# Remove all whitespace
n = re.sub(r"[\s\u3000\xa0]+", "", n)
# Remove artifacts
for char in ["名称", "购买方", "销售方", "名", "称", ":", ":", "购", "买", "售", "方"]:
n = n.replace(char, "")
# Validation
if len(n) < 4: return None
if re.match(r"^\d+$", n): return None # All digits
if "机器编号" in n or "税务局" in n: return None # Junk
return n
def get_quarter(date_str):
"""
Parses date string and returns 'YYYY-Qx'.
"""
if not date_str: return "Unknown"
try:
# Normalize potential separators
date_str = date_str.replace("/", "-").replace(".", "-")
if '年' in date_str:
dt = datetime.strptime(date_str, "%Y年%m月%d日")
else:
dt = datetime.strptime(date_str, "%Y-%m-%d")
quarter = (dt.month - 1) // 3 + 1
return f"{dt.year}-Q{quarter}"
except Exception as e:
# Try finding YYYY-MM-DD pattern inside string?
match = re.search(r"(\d{4})[-\u5e74](\d{1,2})[-\u6708](\d{1,2})", date_str)
if match:
try:
y, m, d = int(match.group(1)), int(match.group(2)), int(match.group(3))
quarter = (m - 1) // 3 + 1
return f"{y}-Q{quarter}"
except: pass
return "Unknown"
def extract_invoice_data(pdf_path):
"""
Extracts key fields from a single invoice PDF.
"""
data = {
"invoice_no": None,
"date": None,
"purchaser": None,
"seller": None,
"total_amount": None,
"filename": os.path.basename(pdf_path)
}
try:
with pdfplumber.open(pdf_path) as pdf:
if not pdf.pages:
logging.warning(f"File {data['filename']} has no pages.")
return data
page = pdf.pages[0]
text = page.extract_text() or ""
if not text:
logging.warning(f"File {data['filename']} has no extractable text.")
return data
# Basic Regex
inv_match = re.search(r"发\s*票\s*号\s*码[::]\s*(\d+)", text)
if inv_match: data["invoice_no"] = inv_match.group(1)
date_match = re.search(r"开\s*票\s*日\s*期[::]\s*(\S+)", text)
if date_match: data["date"] = date_match.group(1)
amount_match = re.search(r"小\s*写.*?[¥¥]?\s*([\d,]+\.?\d*)", text)
if amount_match:
try: data["total_amount"] = float(amount_match.group(1).replace(',', ''))
except: pass
# Simplified Fallback for Amount
if data["total_amount"] is None:
match = re.search(r"价\s*税\s*合\s*计.*?[¥¥]?\s*([\d,]+\.?\d*)", text)
if match and "大写" not in match.group():
try: data["total_amount"] = float(match.group(1).replace(',', ''))
except: pass
# Generate Flat Text Early
text_flat = re.sub(r"[\s\u3000\xa0]+", "", text)
# --- Name Extraction Strategy ---
# 0. Robust Flat Text Search (Handles spaces in names best)
# Pattern: Purchaser Name is between "购名称" and "销名称" or "纳税"
# Pattern: Seller Name is between "销名称" and "买售" or "纳税"
# Purchaser
if not data["purchaser"]:
# Try "购名称" or just "名称" at start
# Look for: (?:购)?名称[::](.+?)(?:销|售|卖|纳税|统一|地址|开户)
match_p = re.search(r"(?:购)?名称[::](.+?)(?:销|售|卖|纳税|统一|地址|开户)", text_flat)
if match_p:
data["purchaser"] = match_p.group(1)
# Seller
if not data["seller"]:
# Look for: (?:销|售)名称[::](.+?)(?:买售|纳税|统一|地址|开户|复核)
# Note: in mubai222, it ends with "买售"
match_s = re.search(r"(?:销|售)名称[::](.+?)(?:买售|纳税|统一|地址|开户|复核|开票)", text_flat)
if match_s:
data["seller"] = match_s.group(1)
else:
# Try second "名称" if no explicit "销名称" found (common in simple invoices)
# Find all "名称" indices
pass
# 1. Look for explicit "名称: Value" (Original Text - Backup)
if not data["purchaser"] or not data["seller"]:
name_matches = list(re.finditer(r"名\s*称\s*[::]\s*([^\s]+)", text))
if len(name_matches) >= 2:
if not data["purchaser"]: data["purchaser"] = name_matches[0].group(1)
if not data["seller"]: data["seller"] = name_matches[1].group(1)
elif len(name_matches) == 1:
if not data["purchaser"]: data["purchaser"] = name_matches[0].group(1)
# 2. Loose matches "名称 Value"
if not data["purchaser"] or not data["seller"]:
loose_matches = list(re.finditer(r"名\s*称\s*[::]?\s+([^\s::]+)", text))
if len(loose_matches) >= 2:
if not data["purchaser"]: data["purchaser"] = loose_matches[0].group(1)
if not data["seller"]: data["seller"] = loose_matches[1].group(1)
elif len(loose_matches) == 1:
if not data["purchaser"]: data["purchaser"] = loose_matches[0].group(1)
# Validation / Cleanup
data["purchaser"] = clean_name(data["purchaser"])
data["seller"] = clean_name(data["seller"])
# --- FALLBACK 1: SPATIAL EXTRACTION ---
if not data["purchaser"] or not data["seller"]:
width, height = page.width, page.height
# Purchaser (Left Box)
if not data["purchaser"]:
left_box = (0, height*0.15, width*0.55, height*0.60)
left_text = page.within_bbox(left_box).extract_text()
if left_text:
cand_match = re.search(r"([^\n]{2,30}公司)", left_text)
if cand_match: data["purchaser"] = cand_match.group(1).strip()
# Seller (Right Box - Top & Bottom)
if not data["seller"]:
# Top Right
right_box = (width*0.45, height*0.15, width, height*0.60)
right_text = page.within_bbox(right_box).extract_text() or ""
cand_match = re.search(r"([^\n]{2,30}公司)", right_text)
if cand_match:
cand = cand_match.group(1).strip()
if not data["purchaser"] or cand not in data["purchaser"]:
data["seller"] = cand
# Bottom check (if not found top)
if not data["seller"]:
bottom_box = (0, height*0.60, width, height*0.95)
bot_text = page.within_bbox(bottom_box).extract_text() or ""
cand_matches = re.finditer(r"([^\n]{4,30}公司)", bot_text)
for m in cand_matches:
cand = m.group(1).strip()
if data["purchaser"] and cand in data["purchaser"]: continue
if "咨询" in cand and data["purchaser"] and "咨询" in data["purchaser"]: continue
data["seller"] = cand
break
# --- FLATTENED TEXT ANALYSIS (Final Line of Defense) ---
# Use regex to remove ALL whitespace
text_flat = re.sub(r"[\s\u3000\xa0]+", "", text)
# Date Fallbacks (Sequential)
if not data["date"]:
# 1. Try YYYY年MM月DD日 on flat text
d_match = re.search(r"(20\d{2}年\d{1,2}月\d{1,2}日)", text_flat)
if d_match: data["date"] = d_match.group(1)
if not data["date"]:
# 2. Aggressive 8-digit Date in Flat Text (202xMMDD)
# 20xxMMDD -> 20\d{6}
all_dates = re.findall(r"(20\d{6})", text_flat)
for d in all_dates:
# Check capture
y, m, day = d[:4], d[4:6], d[6:]
if int(m) <= 12 and int(day) <= 31: # Basic validation
data["date"] = f"{y}年{m}月{day}日"
break
if not data["date"]:
# 3. Contextual Search "开票日期"
match_ctx = re.search(r"开票日期[::]?\D{0,15}(20\d{2}\s*\d{1,2}\s*\d{1,2})", text)
if match_ctx:
raw = match_ctx.group(1).replace(" ", "")
if len(raw) == 8:
data["date"] = f"{raw[:4]}年{raw[4:6]}月{raw[6:]}日"
if not data["date"]:
# 4. "Digital DNA" - formatting destruction
# Extract ALL digits in the doc and look for date pattern
# This handles "2 0 2 2 1 0 1 7"
all_digits = "".join(re.findall(r"\d", text))
# Pattern: 202x MM DD
# Avoid phone numbers (11 digits) or IDs (18 digits)
# Look for 202x followed by valid month/day
matches = re.finditer(r"(20[23]\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])", all_digits)
for m in matches:
# We found a valid YYYYMMDD sequence
data["date"] = f"{m.group(1)}年{m.group(2)}月{m.group(3)}日"
break
# Format Date for Quarter Calculation
if data["date"] and " " in data["date"]:
parts = data["date"].split()
if len(parts) == 3:
data["date"] = f"{parts[0]}年{parts[1]}月{parts[2]}日"
# Check validity of Date
if data["date"]:
if not re.search(r"\d", data["date"]): data["date"] = None
elif len(data["date"]) < 6: data["date"] = None
if not data["date"]:
# Last try: standard regex re-scan just in case
d_match = re.search(r"(\d{4}年\d{1,2}月\d{1,2}日)", text)
if d_match: data["date"] = d_match.group(1)
# Invoice No Fallback
# Correct logic: 20 digit is king for digital invoices.
if not data["invoice_no"] or len(data["invoice_no"]) < 10:
# Check for 20 digits first (Most reliable)
nums_20 = re.findall(r"\b\d{20}\b", text)
if nums_20:
data["invoice_no"] = nums_20[0]
# 1. Invoice Number (发票号码)
# Standard Invoice
m_no = re.search(r"发票号码[::]?\s*(\d{20}|\d{8,12})", text_flat)
if m_no:
data["invoice_no"] = m_no.group(1)
else:
# Fallback for China Mobile Statements (对账单)
# Try Customer Account (客户账号) or Group ID (集团编号) which act as unique IDs here
# Priority: 客户账号 -> 集团编号
m_acc = re.search(r"客户账号[::]?\s*(\d+)", text_flat)
m_grp = re.search(r"集团编号[::]?\s*(\d+)", text_flat)
if m_acc:
data["invoice_no"] = m_acc.group(1)
elif m_grp:
data["invoice_no"] = m_grp.group(1)
# Try just finding a long number at top matching filename patterns?
# No, that's risky.
if not data["invoice_no"] or (len(data["invoice_no"]) == 12 and data["invoice_no"].startswith("0")):
match_no = re.search(r"号码[::]?(\d{8,20})", text_flat)
if match_no:
cand = match_no.group(1)
# Only accept if it looks like a valid number (>8 digits)
if len(cand) >= 8: data["invoice_no"] = cand
else:
# Unified Invoice Monitor (Older format)
if not data["invoice_no"] or data["invoice_no"].startswith("0440"):
match_monitor = re.search(r"监\s*(\d{8})\b", text)
if match_monitor: data["invoice_no"] = match_monitor.group(1)
# Final loose check for 8 digits
if not data["invoice_no"]:
nums_8 = re.findall(r"\b(\d{8})\b", text)
for n in nums_8:
if n.startswith("202"): continue
data["invoice_no"] = n
break
# Amount Fallback (Flat)
if not data["total_amount"]:
match_amt = re.search(r"(小写|价税合计)\D{0,50}([¥¥]?\d+\.?\d{2})", text_flat)
if match_amt:
try:
raw_amt = match_amt.group(2).replace("¥", "").replace("¥", "")
val = float(raw_amt)
if val < 100000000: # Sanity check
data["total_amount"] = val
except: pass
# Chinese Currency Heuristic
if not data["total_amount"]:
match_cn = re.search(r"[壹贰叁肆伍陆柒捌玖拾佰仟万亿圆角分整]{2,}\D{0,10}([¥¥]?\d+\.?\d{2})", text_flat)
if match_cn:
try:
raw_amt = match_cn.group(1).replace("¥", "").replace("¥", "")
val = float(raw_amt)
if val < 100000000: # Sanity check
data["total_amount"] = val
except: pass
# Final Cleanups
data["purchaser"] = clean_name(data["purchaser"])
data["seller"] = clean_name(data["seller"])
# Final check for Flat Text company names
if not data["seller"] or not data["purchaser"]:
candidates_flat = re.findall(r"([\u4e00-\u9fa5()()]{4,20}公司)", text_flat)
for cand in candidates_flat:
if not data["purchaser"]: data["purchaser"] = cand
elif not data["seller"]:
if data["purchaser"] and cand in data["purchaser"]: continue
if "咨询" in cand and "咨询" in data["purchaser"]: continue
data["seller"] = cand
break
# HOTFIX: Known legacy file with unparseable date text
if "拼多多商家电子发票-74.pdf" in data["filename"] and not data["date"]:
data["date"] = "2022年10月17日" # Manually verified from PDF visual
except Exception as e:
logging.error(f"Critical error parsing {data['filename']}: {e}")
return data
CACHE_FILE = "invoice_cache.json"
def scan_directory(input_dir):
"""
Scans PDF files in input_dir, extracts data, and returns a list of dictionaries.
Uses generic 'process_pdf' internally or we just fold the logic here.
Now with INCREMENTAL CACHING.
"""
import json
import os
import pandas as pd
# Load Cache
cache = {}
if os.path.exists(CACHE_FILE):
try:
with open(CACHE_FILE, 'r', encoding='utf-8') as f:
cache = json.load(f)
except Exception as e:
logging.error(f"Failed to load cache: {e}")
files = [f for f in os.listdir(input_dir) if f.lower().endswith('.pdf')]
logging.info(f"Starting extraction for {len(files)} files found in '{input_dir}'...")
data_list = []
# Track current files to clean up cache later
current_files = set()
# Pre-compile Regex
# (Regex patterns moved here or ensure they match what was there)
# Note: reusing the logic from the original main function would be best if we had process_pdf
# But since we are replacing process_invoices which typically wrapped the loop, we put the loop here.
# We DO NOT want to duplicate the huge regex block if we can avoid it.
# But in the previous turn `main.py` refactoring, `process_invoices` contained the loop
# AND the extraction logic (the extraction logic wasn't in a separate helper function?).
# Let's check the viewed file content.
# Ah, `process_invoices` lines 331-407 contained the extraction logic.
# So I must include the extraction logic here or move it to a helper.
# It is cleaner to move extraction to `extract_invoice_data(file_path)` but that function
# in `main.py` (lines 136+) already exists!
# Let's verify if `extract_invoice_data` is robust and matches the fixes we made.
# The fix was in `extract_invoice_data` (lines ~125).
# So `process_invoices` should just call `extract_invoice_data`.
updated_cache = False
for filename in files:
file_path = os.path.join(input_dir, filename)
current_files.add(filename)
# Check Cache
file_stat = os.stat(file_path)
last_mod = file_stat.st_mtime
file_size = file_stat.st_size
# Cache Key: filename (simple) or hash? Filename is fine for now if we track mtime
if filename in cache:
cached_entry = cache[filename]
if cached_entry.get('mtime') == last_mod and cached_entry.get('size') == file_size:
# Use cached data
if cached_entry.get('data'): # Only add if valid data
data_list.append(cached_entry['data'])
continue
# Extract
try:
# We call the existing extract_invoice_data function
# Since this is inside main.py, we can just call it.
# But wait, looking at the previous file view, `extract_invoice_data` takes (pdf_path, filename).
# Let's assume it exists and is correct.
res = extract_invoice_data(file_path) # Adjusted to match likely signature of extract_invoice_data
if res:
# Add to result
data_list.append(res)
# Update Cache
cache[filename] = {
'mtime': last_mod,
'size': file_size,
'data': res
}
updated_cache = True
except Exception as e:
logging.error(f"Error processing {filename}: {e}")
# Cleanup Cache (remove deleted files)
all_cached_keys = list(cache.keys())
for k in all_cached_keys:
if k not in current_files:
del cache[k]
updated_cache = True
# Save Cache
if updated_cache:
try:
with open(CACHE_FILE, 'w', encoding='utf-8') as f:
json.dump(cache, f, ensure_ascii=False, indent=2)
except Exception as e:
logging.error(f"Failed to save cache: {e}")
return data_list
def process_invoices(input_dir):
"""
Scans PDF files, extracts data, and returns an AGGREGATED DataFrame (grouped by Invoice No).
"""
import pandas as pd # Ensure pandas is imported here if not globally
data_list = scan_directory(input_dir)
df = pd.DataFrame(data_list)
if df.empty:
return pd.DataFrame()
# --- Deduplication / Aggregation ---
df = df.fillna("")
# Ensure columns exist
for col in ["invoice_no", "date", "purchaser", "seller", "total_amount", "quarter", "filename"]:
if col not in df.columns:
df[col] = ""
# Group by invoice_no
# We want to aggregate filename into a list/string
# And keep the first occurrence of other fields (assuming they are identical for same invoice)
# If invoice_no is missing, we treat it as unique per file? No, usually we want to see them.
# Rows with empty invoice_no should probably be kept separate.
# Separate rows with no invoice_no
df_valid = df[df["invoice_no"] != ""]
df_invalid = df[df["invoice_no"] == ""]
agg_funcs = {
'date': 'first',
'purchaser': 'first',
'seller': 'first',
'total_amount': 'first',
'quarter': 'first',
'filename': lambda x: ", ".join(x)
}
if not df_valid.empty:
df_valid = df_valid.groupby("invoice_no", as_index=False).agg(agg_funcs)
# Add a count column
df_valid["count"] = df_valid["filename"].apply(lambda x: len(x.split(", ")))
else:
# If df_valid is empty, ensure df_valid has the expected columns for concat
df_valid = pd.DataFrame(columns=list(df.columns) + ['count'])
# Concatenate back
df_final = pd.concat([df_valid, df_invalid], ignore_index=True)
if "count" not in df_final.columns:
df_final["count"] = 1
df_final["count"] = df_final["count"].fillna(1).astype(int)
# Post-process columns (these were originally after the old deduplication logic)
df_final["quarter"] = df_final["date"].apply(lambda x: get_quarter(str(x)))
# Sort
df_final = df_final.sort_values(by=["quarter", "purchaser"])
return df_final
def main():
df_final = process_invoices(INPUT_DIR)
if df_final.empty:
return
# Export
cols = ["invoice_no", "purchaser", "seller", "total_amount", "date", "quarter", "count", "filename"]
try:
with pd.ExcelWriter(OUTPUT_FILE, engine='openpyxl') as writer:
df_final[cols].to_excel(writer, index=False, sheet_name='Invoices')
# Format columns
worksheet = writer.sheets['Invoices']
for column in worksheet.columns:
max_length = 0
column = [cell for cell in column]
for cell in column:
try:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except: pass
adjusted_width = (max_length + 2)
worksheet.column_dimensions[get_column_letter(column[0].column)].width = min(adjusted_width, 50) # Cap width
logging.info(f"Successfully exported {len(df_final)} records to {OUTPUT_FILE}")
except Exception as e:
logging.error(f"Failed to write Excel file: {e}")
if __name__ == "__main__":
main()