Spaces:
Sleeping
Sleeping
Download tests/generators/make_india_blank_dates.py from rudratoshs/exportstatement: direct link, hf CLI and curl.
- Browser
- Download file 4.89 kB
-
https://huggingface.co/spaces/rudratoshs/exportstatement/resolve/main/tests/generators/make_india_blank_dates.py
- Command line
-
hf download hf://spaces/rudratoshs/exportstatement/tests/generators/make_india_blank_dates.py
-
curl -L -o make_india_blank_dates.py https://huggingface.co/spaces/rudratoshs/exportstatement/resolve/main/tests/generators/make_india_blank_dates.py
4.89 kB
| """ | |
| make_india_blank_dates.py — synthetic copy of a common Indian e-statement layout | |
| (Standard Chartered style), with made-up data. It reproduces the traits that broke | |
| the converter on a real statement: | |
| - the table is only framed (no row rules), so pdfplumber returns one giant row | |
| - left-aligned headers above right-aligned amounts, in columns whose whitespace | |
| gaps don't line up with the headers | |
| - same-day transactions leave the Date and Value Date cells blank | |
| - descriptions wrap over several lines with no amounts | |
| - "dd Mon yy" dates, a BALANCE FORWARD row at the top of every page, | |
| a TOTAL row, and a disclaimer footer below the table | |
| Writes tests/fixtures/hard/t9_india_blank_dates.pdf | |
| """ | |
| from __future__ import annotations | |
| import random | |
| from pathlib import Path | |
| from reportlab.lib.pagesizes import A4 | |
| from reportlab.pdfgen import canvas | |
| OUT = Path(__file__).resolve().parent.parent / "fixtures" / "hard" / "t9_india_blank_dates.pdf" | |
| random.seed(19) | |
| NARR = [ | |
| ["UPI/{n}/", "PAYTM/ADD-MONEY@PAYTM/PYTM0123456/", "{n}/"], | |
| ["ATM WITHDRAWAL SELF-SWITCH", "AT NFS 20:10:24/{n}", "00000000150000/INR"], | |
| ["PURCHASE SAMPLE STORE", "CHENNAI IN 10:13:08/726630"], | |
| ["NEFT SAMPLE EMPLOYER PVT LTD", "SALARY"], | |
| ["CGST @ 9.00%"], | |
| ["IMPS/P2A/{n}/0350015", "SAMPLE PAYEE"], | |
| ] | |
| def money(c: int) -> str: | |
| return f"{c / 100:,.2f}" | |
| def make_rows(n=34, opening=11_445_365): | |
| rows, bal, day = [], opening, 3 | |
| for i in range(n): | |
| if random.random() < 0.55: | |
| day += 1 | |
| credit = random.random() < 0.3 | |
| amt = random.randint(1_000, 2_500_000 if credit else 400_000) | |
| bal += amt if credit else -amt | |
| lines = [s.format(n=917400000000 + i) for s in random.choice(NARR)] | |
| rows.append({"day": min(day, 28), "lines": lines, "cr": amt if credit else 0, | |
| "dr": 0 if credit else amt, "bal": bal}) | |
| return rows, opening, bal | |
| def draw(): | |
| rows, opening, closing = make_rows() | |
| c = canvas.Canvas(str(OUT), pagesize=A4) | |
| W, H = A4 | |
| X = {"date": 40, "value": 95, "desc": 150, "chq": 330, "dep": 380, "wdl": 445, "bal": 515} | |
| R = {"dep": 425, "wdl": 495, "bal": 565} # right edges of amount columns | |
| lh = 10 | |
| def header(y): | |
| c.setFont("Helvetica-Bold", 8) | |
| c.drawString(X["value"], y + 9, "Value") | |
| for k, t in (("date", "Date"), ("desc", "Description"), ("chq", "Cheque"), | |
| ("dep", "Deposit"), ("wdl", "Withdrawal"), ("bal", "Balance")): | |
| c.drawString(X[k], y, t) | |
| c.drawString(X["value"], y - 9, "Date") | |
| c.setFont("Helvetica", 8) | |
| return y - 24 | |
| def row(y, d, lines, dep="", wdl="", bal=""): | |
| if d: | |
| c.drawString(X["date"], y, d) | |
| c.drawString(X["value"], y, d) | |
| c.drawString(X["desc"], y, lines[0]) | |
| for k, v in (("dep", dep), ("wdl", wdl), ("bal", bal)): | |
| if v: | |
| c.drawRightString(R[k], y, v) | |
| for extra in lines[1:]: | |
| y -= lh | |
| c.drawString(X["desc"] + 6, y, extra) | |
| return y - lh | |
| def footer(page, top): | |
| c.rect(35, top, W - 70, H - 140 - top) # frame only, no row rules | |
| c.setFont("Helvetica", 7) | |
| c.drawString(40, 70, "Bank deposits are insured up to Rs 5 lakh per depositor.") | |
| c.drawString(40, 60, "Report irregularities within 30 days of the statement date.") | |
| c.drawString(40, 40, f"Page {page}") | |
| c.setFont("Helvetica", 8) | |
| c.setFont("Helvetica-Bold", 11) | |
| c.drawString(40, H - 60, "ACCOUNT STATEMENT") | |
| c.setFont("Helvetica", 8) | |
| c.drawString(380, H - 80, "ACCOUNT NO. : 99999990001") | |
| c.drawString(380, H - 92, "CURRENCY INR") | |
| page, y = 1, header(H - 150) | |
| month = "Jun 25" | |
| last_day = None | |
| y = row(y, f"03 {month}", ["BALANCE FORWARD"], bal=money(opening)) | |
| for r in rows: | |
| if y < 140: | |
| footer(page, y) | |
| c.showPage() | |
| page += 1 | |
| c.setFont("Helvetica", 8) | |
| y = header(H - 150) | |
| y = row(y, f"{last_day:02d} {month}", ["BALANCE FORWARD"], bal=money(prev_bal)) | |
| d = f"{r['day']:02d} {month}" if r["day"] != last_day else "" | |
| y = row(y, d, r["lines"], dep=money(r["cr"]) if r["cr"] else "", | |
| wdl=money(r["dr"]) if r["dr"] else "", bal=money(r["bal"])) | |
| last_day, prev_bal = r["day"], r["bal"] | |
| tot_cr = sum(r["cr"] for r in rows) | |
| tot_dr = sum(r["dr"] for r in rows) | |
| c.setFont("Helvetica-Bold", 8) | |
| c.drawString(X["desc"] + 60, y, "TOTAL") | |
| c.drawRightString(R["dep"], y, money(tot_cr)) | |
| c.drawRightString(R["wdl"], y, money(tot_dr)) | |
| c.drawRightString(R["bal"], y, money(closing)) | |
| footer(page, y - 6) | |
| c.save() | |
| return len(rows), tot_cr / 100, tot_dr / 100, closing / 100 | |
| if __name__ == "__main__": | |
| print(OUT, draw()) | |