Skip to content

Instantly share code, notes, and snippets.

@seocam
Last active December 28, 2024 21:20
Show Gist options
  • Select an option

  • Save seocam/27dedc6164f0e0e31295a0ec7fcfa601 to your computer and use it in GitHub Desktop.

Select an option

Save seocam/27dedc6164f0e0e31295a0ec7fcfa601 to your computer and use it in GitHub Desktop.
Converte CSV para um arquivo OFX aceito pela contabilizei
#!/usr/bin/env python
import argparse
import calendar
import itertools as it
from datetime import datetime
from csv2ofx import utils
from csv2ofx.mappings.default import mapping
from csv2ofx.ofx import OFX
from meza.io import IterStringIO, read_csv
def get_start_end(year, month):
last_day = calendar.monthrange(year, month)[1]
start = datetime(year, month, 1, 0, 0, 0)
end = datetime(year, month, last_day, 23, 59, 59)
return start, end
def get_first_record_year_month(filepath):
records = read_csv(filepath)
for record in records:
date = datetime.strptime(record["Date"], "%Y-%m-%d")
return date.year, date.month
def convert_to_ofx(filepath):
year, month = get_first_record_year_month(filepath)
start, end = get_start_end(year, month)
mapping["currency"] = "BRL"
ofx = OFX(mapping, def_type="CHECKING", start=start, end=end)
records = read_csv(filepath)
groups = ofx.gen_groups(records)
trxns = ofx.gen_trxns(groups)
cleaned_trxns = ofx.clean_trxns(trxns)
data = utils.gen_data(cleaned_trxns)
content = it.chain(
[
"OFXHEADER:100\n",
ofx.header(language="POR"),
ofx.gen_body(data),
ofx.footer(),
]
)
return b"\n".join(IterStringIO(content))
def get_parser():
parser = argparse.ArgumentParser(
description="Converte CSV para um OFX importável pela Contabilizei"
)
parser.add_argument("csv_file", help="Path of the input CSV file")
parser.add_argument("ofx_file", help="Path of the output OFX file")
return parser
def run():
args = get_parser().parse_args()
ofx_str = convert_to_ofx(args.csv_file)
with open(args.ofx_file, "w") as output:
output.write(ofx_str.decode("utf-8"))
if __name__ == "__main__":
run()
@f4b10goncalves
Copy link

Também estou com o mesmo problema de enviar OFX para a Contabilizei, só que com o PagBank (PagSeguro). Vou tentar adaptar seu script para consumir o CSV exportado pelo PagBank, que é diferente do exportado pelo NuBank. Você poderia compartilhar um exemplo de arquivo OFX aceito pela Contabilizei, por favor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment