Skip to content

Instantly share code, notes, and snippets.

@serrasqueiro
Last active April 25, 2024 13:49
Show Gist options
  • Save serrasqueiro/c2e1bc13959ec52f2f59b38593dd5ae1 to your computer and use it in GitHub Desktop.
Save serrasqueiro/c2e1bc13959ec52f2f59b38593dd5ae1 to your computer and use it in GitHub Desktop.
IBANs em Portugal

Lista do Banco de Portugal (BdP)

  1. (here) Banco de Portugal - listaiban.xls
  2. Example on parsing those xls (using xlrd):
    • iban_bdp.py, inspired on listarecibos.py
#!/usr/bin/python3
""" IBAN from BdP
Depends on xlrd and on xlibre.xolder -- to handle older Excel formats!
"""
# pylint: disable=missing-function-docstring
import sys
import os
sys.path.append("/home/henrique/anaceo/fhm/external/thirdparty/xlrd")
import xlibre.jsonex
from xlibre.xolder import ABook
def main():
""" The main script, when running, instead of importing """
run_script(sys.argv[1:])
def run_script(args):
print("ARGS:", args, end="<<<\n\n")
if len(args) == 1:
outname = args[0]
elif not args:
outname = "/tmp/output.json"
else:
print("Too many args:", args)
return None
wbk, work = run_get(os.getcwd(), "ListaIBAN.xls")
seq = work
astr = xlibre.jsonex.json_dump(seq)
assert wbk, "Invalid!"
print(astr)
if not outname or outname in (".",):
return
with open(outname, "wb") as fdout:
fdout.write(bytes(astr, "ascii"))
def run_get(path:str, base:str):
""" Read old xls """
wbk = ABook("ListaIBAN")
wbk.load(os.path.join(path, base))
wbk.ibook.first()
cont = wbk.ibook.current.lines()
work = parse(cont, wbk)
return wbk, work
def parse(cont:list, wbk):
""" BdP ListaIBAN.xls parser """
assert cont, wbk.get_aname()
seq = []
for idx, tup in enumerate(cont[1:], 1):
iban, ag_fin, banco, tipo = tup
if not iban.strip():
break
if not iban.isdigit():
break
item = {
"AnId": idx,
"iban": int(iban),
"agency": int(ag_fin),
"bank": banco.strip(),
"type": tipo.strip(),
}
seq.append(item)
seq.append(
{
"AnId": 0,
"iban": None,
"agency": "",
"bank": "",
"type": None,
}
)
return seq
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment