diff --git a/superx_budget/budget.py b/superx_budget/budget.py index de53b49..27cc055 100644 --- a/superx_budget/budget.py +++ b/superx_budget/budget.py @@ -2,6 +2,7 @@ from collections import namedtuple +from .helpers import get_sheet_of_file from .exceptions import BudgetParserError EXPECTED_TABLE_HEADERS = [ @@ -60,3 +61,9 @@ def parse_budget_data(xls_sheet): rows = (ExcelRow(i, v) for i, v in enumerate(xls_sheet.values, start=1)) _check_table_header(next(rows)) return list(_parse_data_table(rows)) + + +def parse_budget_file(file_path): + """ parses the budget file """ + sheet = get_sheet_of_file(file_path, name=None) + return parse_budget_data(sheet) diff --git a/superx_budget/helpers.py b/superx_budget/helpers.py new file mode 100644 index 0000000..eae1278 --- /dev/null +++ b/superx_budget/helpers.py @@ -0,0 +1,15 @@ +""" some helper functions """ + +import openpyxl + + +def get_sheet_of_file(excel_file, name=None): + """ returns a sheet from an excel FileCache + + if name is set to None, the function returns the first sheet + """ + workbook = openpyxl.open(excel_file) + if name is None: + sheets = workbook.sheetnames + name = sheets[0] + return workbook[name] diff --git a/superx_budget/superx.py b/superx_budget/superx.py index 4356b6f..0b2db67 100644 --- a/superx_budget/superx.py +++ b/superx_budget/superx.py @@ -3,6 +3,7 @@ from datetime import datetime from collections import namedtuple +from .helpers import get_sheet_of_file from .exceptions import SuperXParserError EXPECTED_HEADLINE = "Verwendungsnachweis und Kassenstand SAP" @@ -83,3 +84,9 @@ def parse_export_data(xls_sheet): _skip_export_data_until_table_header(rows) data = list(_parse_data_table(rows)) return SuperXResult(metadata.account_year, metadata.export_date, data) + + +def parse_exported_file(file_path): + """ parses the budget file """ + sheet = get_sheet_of_file(file_path, name=None) + return parse_export_data(sheet) diff --git a/tests/test_budget_parser.py b/tests/test_budget_parser.py index 6722571..f08b28a 100644 --- a/tests/test_budget_parser.py +++ b/tests/test_budget_parser.py @@ -82,3 +82,16 @@ def test_parse_budget_data(example_sheet): assert last.number == "=A51+1" assert last.project_name == "ZIM Microcoat II" assert last.rest == "=E54-F54" + + +def test_parse_budget_file(example_file): + from superx_budget.budget import parse_budget_file + + result = parse_budget_file(example_file) + first = result[0] + + assert len(result) == 18 + assert first.row == 3 + assert first.number == 1 + assert first.project_name == "Safegurard I (neu)" + assert first.rest == "=E3-F3" diff --git a/tests/test_superx_parser.py b/tests/test_superx_parser.py index 0cd3dab..ec74eb3 100644 --- a/tests/test_superx_parser.py +++ b/tests/test_superx_parser.py @@ -140,3 +140,16 @@ def test_parse_export_data(example_workbook): assert last.revenue_actual == 2236 assert last.revenue_target == 3236 assert last.acutal_value == 4236 + + +def test_parse_exported_file(example_file): + from superx_budget.superx import parse_exported_file + + result = parse_exported_file(example_file) + first = result.data[0] + + assert len(result.data) == 212 + assert first.cost_center == "1110200121" + assert first.fonds == "3310" + assert first.project == "1100000102" + assert first.kind == "1 - Personal"