A python script to convert Excel files into Sphinx reStructuredText-formatted documentation.
In order to extract data from an Excel sheet and create a reST-formatted document, template files are used. Inside these template files will be a mixture of reST-formatted text and tags used by the python script to populate the template (eg. {loop_start}
.
Example snippet from example_sheet1_template.txt
file:
{loop_start}
{Data Tag}
------------------------------------------------------------
{Description}
:Field Type: {Field Type}
:Units: {Units}
:Source: {Source}
:Logic: {Logic}
{loop_end}
In the above snippet, {Data Tag}
is an example column header in the Excel sheet "test_worksheet.xlsx", as are {Field Type}
, {Units}
, {Source}
, and {Logic}
. The python script will look for any tags matching a column header (inside brackets { } ) and substitute the actual cell contents.
To populate ALL rows from the Excel sheet into the template file, a loop is specified by the tags {loop_start}
and {loop_end}
. The script will duplicate the lines between these two tags -- one duplicate for each row in a sheet. Then, row-by-row, the script will replace tags matching each column header with actual cell values.
These are the functions found inside populate_templates_from_sheets.py
-
Returns a string representing the indent of a string found within a string.
-
Adds an indent string before each line. Default num_lines=0 is all lines. indentation_string eg. '\t\t' or ' '
-
Returns the string found between two strings, eg. '{start}}' and '{end}'
-
Prevents characters in a string from being interpreted as reST formatting.
-
Returns a 2D list (list[rows][columns]) of cell values from an Excel sheet.
-
Returns a comma-separated string of cell values, given 2D list of cells (eg. '"cell1A", "cell1B"\n# \t\t"cell2A", "cell2B"')
Indents each line to keep reST formatting for csv tables. -
Returns a comma-separated string of header names, given 2D list of cells.
eg. '"header1", "header2", "header3"' -
Replaces data tags (eg. "{Description}") in template file with Excel values.