|
1 | 1 | # xl-engine |
2 | 2 | Use your existing Excel workbooks like they were Python programs: A utility built on XLWings to automate the input, execution, and data retrieval of your existing Excel Workbooks. |
| 3 | + |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +`pip install xl-engine` |
| 8 | + |
| 9 | +## Basic Usage |
| 10 | + |
| 11 | +### `execute_workbook` |
| 12 | + |
| 13 | +Use `execute_workbook` when you want to execute a workbook once with known, provided parameters. |
| 14 | + |
| 15 | +```python |
| 16 | +import xl_engine as xl |
| 17 | + |
| 18 | +results = xl.execute_workbook( |
| 19 | + TEST_DATA_DIR / "example_wb.xlsx", |
| 20 | + cells_to_change = {"B1": 33, "B2": 66}, |
| 21 | + cells_to_retrieve=['B4', 'B5'], # This can either be a list or a dict; see next example |
| 22 | + sheet_idx=1, |
| 23 | + new_filepath=TEST_DATA_DIR / "stored_results.xlsx" |
| 24 | +) |
| 25 | + |
| 26 | +# results |
| 27 | +# {'B4': 22.0, 'B5': 11.0} |
| 28 | +``` |
| 29 | + |
| 30 | +You can also use a dictionary in `cells_to_retrieve` to meaningfully label the results: |
| 31 | + |
| 32 | +```python |
| 33 | +results2 = xl.execute_workbook( |
| 34 | + TEST_DATA_DIR / "example_wb.xlsx", |
| 35 | + cells_to_change = {"B1": 33, "B2": 66}, |
| 36 | + cells_to_retrieve={'B4': "label1", 'B5': "label2"}, # Now a dict |
| 37 | + sheet_idx=1, |
| 38 | + new_filepath=TEST_DATA_DIR / "stored_results.xlsx" |
| 39 | +) |
| 40 | + |
| 41 | +# results2 |
| 42 | +# {'label1': 44.0, 'label2': 39.599999999999994} |
| 43 | +``` |
| 44 | + |
| 45 | +### `excel_runner` |
| 46 | + |
| 47 | +Use `excel_runner` when you want to execute a workbook multiple times (static inputs) with multiple options (dynamic inputs). |
| 48 | + |
| 49 | +```python |
| 50 | +import xl_engine as xl |
| 51 | + |
| 52 | +# Creates a callable with the following function signature: callable(x: float | int) -> bool: |
| 53 | +# When a value is a passed to dcr2, i.e. dcr2(some_value), it will return True if |
| 54 | +# the value is greater-than-or-equal-to 2 |
| 55 | +dcr2 = xl.create_condition_check(2, "ge") |
| 56 | + |
| 57 | +# static_inputs will be used to populate each workbook. |
| 58 | +# Within each set of static inputs, a second sub-iteration will occur |
| 59 | +# where the dynamic inputs will be input allowing you to test the |
| 60 | +# results for different "options" of dynamic inputs. |
| 61 | +# If the save conditions pass (return True for all keys), then a copy of the |
| 62 | +# workbook will be saved to disk with the successful inputs recorded. |
| 63 | +# A dictionary of all calculated results will be returned. |
| 64 | +results = xl.excel_runner( |
| 65 | + TEST_DATA_DIR / "example_wb.xlsx", |
| 66 | + static_inputs={"B1": [10, 20], "Labels": ["C01", "C02"]}, |
| 67 | + dynamic_inputs={ |
| 68 | + "OptA": {"B2": 22}, |
| 69 | + "OptB": {"B2": 33}, |
| 70 | + "OptC": {"B2": 55}, |
| 71 | + }, |
| 72 | + save_conditions={"B6": dcr2}, |
| 73 | + static_identifier_keys=["Labels"], |
| 74 | + result_labels={"B6": "meaningful_value"}, |
| 75 | + save_dir=TEST_DATA_DIR / "design" |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +Return value (for the "example_wb.xlsx" file included in the tests directory): |
| 80 | + |
| 81 | +```python |
| 82 | +{ |
| 83 | + 'C01': { |
| 84 | + 'successful_key': None, # None of the options were successful for this case |
| 85 | + 'OptA': {'meaningful_value': 1.2121212121212122}, |
| 86 | + 'OptB': {'meaningful_value': 0.8080808080808081}, |
| 87 | + 'OptC': {'meaningful_value': 0.48484848484848486} |
| 88 | + }, |
| 89 | + 'C02': { |
| 90 | + 'successful_key': 'OptA', |
| 91 | + 'meaningful_value': 2.4242424242424243 |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
0 commit comments