Download parser.py from echosta/Transkrip: direct link, hf CLI and curl.
- Browser
- Download file 3.86 kB
-
https://huggingface.co/spaces/echosta/Transkrip/resolve/6cbc0d5073a3c2a96fccdf4c0b0bc059d5400a5e/parser.py
- Command line
-
hf download hf://spaces/echosta/Transkrip@6cbc0d5073a3c2a96fccdf4c0b0bc059d5400a5e/parser.py
-
curl -L -o parser.py https://huggingface.co/spaces/echosta/Transkrip/resolve/6cbc0d5073a3c2a96fccdf4c0b0bc059d5400a5e/parser.py
3.86 kB
| import pdfplumber, pandas as pd, re, os | |
| from io import BytesIO | |
| from typing import List, Tuple | |
| # --- helpers --- | |
| def _clean_text(s: str) -> str: | |
| if s is None: return "" | |
| s = s.replace("\uFFFD","-").replace("","-").replace("","-") | |
| s = s.replace("\xa0"," ").replace("\n"," ").replace("\r"," ") | |
| s = re.sub(r"\s+", " ", s).strip() | |
| return s | |
| def _clean_grade(s: str) -> str: | |
| s = _clean_text(s).upper() | |
| s = s.replace("A–","A-").replace("B–","B-").replace("C–","C-") | |
| s = s.replace("A+","A+").replace("B+","B+").replace("C+","C+") | |
| s = s.replace("A−","A-").replace("B−","B-").replace("C−","C-") | |
| m = re.search(r"[ABCD](\+|-)?", s) | |
| return m.group(0) if m else s | |
| def _clean_int(s: str) -> str: | |
| s = _clean_text(s) | |
| m = re.search(r"\d+", s) | |
| return m.group(0) if m else s | |
| # --- extractors --- | |
| def _try_table_extract(page) -> List[Tuple[str,str,str,str]]: | |
| rows = [] | |
| settings = dict(vertical_strategy="lines", horizontal_strategy="lines", intersection_tolerance=5) | |
| try: | |
| tables = page.extract_tables(settings) or [] | |
| except Exception: | |
| tables = [] | |
| for t in tables: | |
| if not t: continue | |
| for i, r in enumerate(t): | |
| if i == 0: # header | |
| continue | |
| cells = [(c or "").strip() for c in r] | |
| if len(cells) == 4: | |
| no,mk,sks,nl = cells | |
| if mk: rows.append((_clean_int(no), _clean_text(mk), _clean_int(sks), _clean_grade(nl))) | |
| elif len(cells) == 8: | |
| no,mk,sks,nl,no2,mk2,sks2,nl2 = cells | |
| if mk: rows.append((_clean_int(no), _clean_text(mk), _clean_int(sks), _clean_grade(nl))) | |
| if mk2: rows.append((_clean_int(no2), _clean_text(mk2), _clean_int(sks2), _clean_grade(nl2))) | |
| return rows | |
| def _regex_lines(page) -> List[Tuple[str,str,str,str]]: | |
| rows = [] | |
| txt = (page.extract_text() or "").replace("\r"," ").replace("\xa0"," ") | |
| pat = re.compile(r"^\s*(\d{1,3})\s+([A-Za-z0-9().,\- /&]+?)\s+(\d{1,2})\s+([A-D][\+\-]?)\s*$", re.MULTILINE) | |
| for m in re.finditer(pat, txt): | |
| no,mk,sks,nl = m.groups() | |
| rows.append((_clean_int(no), _clean_text(mk), _clean_int(sks), _clean_grade(nl))) | |
| return rows | |
| # --- public API --- | |
| def parse_transcript_pdf(pdf_bytes: bytes): | |
| """Return list of tuples: (No, Mata Kuliah, SKS, Nilai).""" | |
| rows = [] | |
| with pdfplumber.open(BytesIO(pdf_bytes)) as pdf: | |
| for page in pdf.pages: | |
| part = _try_table_extract(page) | |
| if not part or len(part) < 5: | |
| part = _regex_lines(page) | |
| rows.extend(part) | |
| # dedupe (No, MK) | |
| seen=set(); uniq=[] | |
| for no,mk,sks,nl in rows: | |
| key=(no,mk) | |
| if key in seen: continue | |
| seen.add(key); uniq.append((no,mk,sks,nl)) | |
| return uniq | |
| def write_to_excel(df: pd.DataFrame, out_target, template_bytes: bytes | None = None): | |
| """ | |
| out_target: path string ATAU stream (BytesIO/file-like). | |
| Jika template ada: isi sheet aktif, header A1:D1, data mulai A2. | |
| """ | |
| def _writer(target): # path atau stream | |
| return pd.ExcelWriter(target, engine="openpyxl") | |
| if template_bytes: | |
| try: | |
| import openpyxl | |
| from openpyxl.utils.dataframe import dataframe_to_rows | |
| wb = openpyxl.load_workbook(BytesIO(template_bytes)) | |
| ws = wb.active | |
| ws["A1"].value, ws["B1"].value, ws["C1"].value, ws["D1"].value = "No","Mata Kuliah","SKS","Nilai" | |
| ws.delete_rows(2, 2000) | |
| for r in dataframe_to_rows(df, index=False, header=False): | |
| ws.append(r) | |
| wb.save(out_target) | |
| return | |
| except Exception: | |
| pass # fallback file baru | |
| with _writer(out_target) as xw: | |
| df.to_excel(xw, index=False, sheet_name="Transkrip") | |