Skip to content

Instantly share code, notes, and snippets.

@justinTM
Created September 29, 2021 01:00
Show Gist options
  • Save justinTM/9ce347c98f2d2a1a65d96374ee69ed27 to your computer and use it in GitHub Desktop.
Save justinTM/9ce347c98f2d2a1a65d96374ee69ed27 to your computer and use it in GitHub Desktop.
Confluence will not show the true underlying string contents as Confluence Storage Format. This helps.
import os
from atlassian import Confluence
CONFLUENCE = Confluence(
url=f"https://{os.environ['CONFLUENCE_SITE']}.atlassian.net",
username=os.environ['CONFLUENCE_USER'],
password=os.environ['CONFLUENCE_TOKEN'],
cloud=True)
def get_confluence_page_source(title: str = None, page_id: int = None):
"""
Retrieve a string representing the body of a Confluence page as Confluence Storage Format.
see: https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html
Useful to get macros and other formatting not found in the normal body contents.
Parameters:
title : (optional if page_id is given) the name of the page to retrieve (must have environment variable "CONFLUENCE_SPACE" set)
page_id : (optional if title is given) the page ID to retrieve, eg. 28280848470
Examples:
>>> CONFLUENCE = Confluence(
url=f"https://{os.environ['CONFLUENCE_SITE']}.atlassian.net",
username=os.environ['CONFLUENCE_USER'],
password=os.environ['CONFLUENCE_TOKEN'],
cloud=True)
>>> get_confluence_page_source(page_id=os.environ['CONFLUENCE_PARENT_PAGE_ID'])
'<ac:structured-macro ac:name="gallery" ac:schema-version="1" data-layout="full-width" ac:local-id="c2b8a095-a706-4752-a4ab-928de77c394e" ac:macro-id="3d51afcda9cf97aa41da0694cec0f5ad"><ac:parameter ac:name="columns">10</ac:parameter><ac:parameter ac:name="includeLabel">container_overview.json</ac:parameter><ac:parameter ac:name="sort">name</ac:parameter></ac:structured-macro><p />'
"""
args = {'expand': 'body.storage'}
if title is None and page_id is None:
raise ValueError('must specify either title or page_id')
elif title is not None:
r = CONFLUENCE.get_page_by_title(title=title, space=CONFLUENCE_SPACE, expand='body.storage')
elif page_id is not None:
r = CONFLUENCE.get_page_by_id(id=page_id, expand='body.storage')
return r['body']['storage']['value']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment