Search code examples
pythonpandasdataframewiki-markup

Converting pandas dataframe to wiki markup table


I'm automating some data processing and creating jira tickets out of it. Pandas does have to_html or to_csv or even to_markdown. But jira supports only wiki markup for creating a table.

e.g.

<!-- wiki markup -->
||header1||header2||header3||\r\n|cell 11|cell 12|cell 13|\r\n|cell 21|cell 22|cell 23|

will create

header1 header2 header3
cell 11 cell 12 cell 13
cell 21 cell 22 cell 23

Is there anyway to convert pandas dataframe to wiki markup table to be used in Jira?

I'm keeping df.iterrows as Last resort since iterating over dataframe is not a recommended solution as per answers in How can I iterate over rows in a Pandas DataFrame? Since my expected dataframe is small, iteration should be fine in my case. This question can be considered as more of a curiosity what can be done in case of larger dataframes.


Solution

  • Don't reinvent the wheel, tabulate supports a jira template:

    from tabulate import tabulate
    
    tabulate(df, headers='keys', tablefmt='jira', showindex=False)
    

    Output:

    '|| header1   || header2   || header3   ||\n| cell 11   | cell 12   | cell 13   |\n| cell 21   | cell 22   | cell 23   |'
    

    If you really want the \r\n line separator:

    tabulate(df, headers='keys', tablefmt='jira', showindex=False).replace('\n', '\r\n')