pythonpython-2.7confluenceconfluence-rest-apiatlassian-python-api

Preserve new lines in Confluence API


I am having the list of dictionaries:

{'associations': [{'host1': ['v1','v2']}, {'host2': ['v2,v3']}, {'host3': ['v1', 'v7']}]}

This list of dictionaries I am trying to pass to confluence page in YAML format by using atlassian-python-api module:

confluence_data = yaml.safe_dump(confluence_data['associations'], encoding='utf-8', allow_unicode=True)

    confluence = Confluence(
        url='https://confluence-url',
        username='user',
        password='password')

    status = confluence.update_or_create(parent_id=someid, title='Test page', body=confluence_data, representation='storage')

But it is being published without new lines on confluence.

I tried to use pprint for adding new lines, but it didn't help as well since it in that case it publishes empty page:

confluence_data = pprint.pprint(confluence_data, width=1)

What I do wrong here?


Solution

  • I do not have the resources to test this directly with a Confluence instance, but if you leave out the utf-8 encoding, you should receive a string. From the atlassian-python-api documentation, I understand that you can then directly use the body parameter with the string as value.

    Try this instead:

    confluence_data = yaml.safe_dump(confluence_data['associations'], allow_unicode=True)
    

    If this did not help, because the atlassian-python-api might not handle the \n correctly, you can try to modify the string before handing it over to confluence by putting each line of the string in the following HTML tags: <p>line</p>

    For example (you could use list comprehension instead to make this more concise):

    new_confluence_data = ""
    confluence_data_lines = confluence_data.splitlines()
    for line in confluence_data_lines:
        line = "<p>" + line + "</p>"
        new_confluence_data += line
    

    Then replace the confluence_data variable with new_confluence_data in your call:

    status = confluence.update_or_create(parent_id=someid, title='Test page', body=new_confluence_data, representation='storage')
    

    Hope this helps or at least gets you on the right track!