I am working on automating Confluence page creation using Python. While I can create pages and retrieve templates, I’m struggling to apply an existing template (blueprint) to a newly created or existing Confluence page.
Setup
I’m using the Atlassian Python API (atlassian-python-api) to interact with Confluence.
Goal
•Create a page in Confluence.
•Apply a template (contentTemplateId) to that page.
Code Overview
1. Creating a Page
def create_page(self, title: str, space_key: str, content: str, parent_id: str = None):
page_params = {
'type': 'page',
'title': title,
'space': space_key,
'body': {
'storage': {
'value': content,
'representation': 'storage'
}
}
}
if parent_id:
page_params['ancestors'] = [{'id': parent_id}]
try:
new_page = self.confluence.create_page(**page_params)
print(f"Page created with ID: {new_page['id']}")
return new_page['id']
except Exception as e:
print(f"Error creating page: {e}")
return None
2. Applying a Template to an Existing Page
I tried using set_page_property to attach the template ID to the page:
def set_template_to_page(self, page_id: str, template_id: str):
try:
self.confluence.set_page_property(
page_id=page_id,
key='contentTemplateId',
data={'value': template_id}
)
print(f"Template {template_id} applied to page {page_id}")
except Exception as e:
print(f"Error applying template: {e}")
3. Error Encountered
Despite setting the contentTemplateId, the template doesn’t apply. The page is created but without the template content or structure.
I also tried retrieving the page to verify if the template was attached:
def get_page_details(self, page_id: str):
try:
page = self.confluence.get_page_by_id(page_id)
print(page)
except Exception as e:
print(f"Error retrieving page: {e}")
But the output shows content_template_id=None and no blueprint attached:
id='1855651970' title='Page with template' space_key='RED' content=None version=1content_template_id=None blueprint=None
Additionally, I encountered this error while attempting different approaches:
Error creating page: java.lang.NullPointerException: Cannot invoke "Object.hashCode()"because "<parameter1>" is null
What I’ve Tried
• Used set_page_property() with contentTemplateId.
•Attempted to include metadata in create_page() (unsupported).
•Verified template IDs by listing all available templates.
The Question
Has anyone successfully applied a Confluence template (blueprint) to a page using the API?
If so, how can I properly associate a template with a page in Confluence?
Environment
• Python Library: atlassian-python-api
•Confluence Version: Cloud (or Server/DC, specify here)
• API Call: POST /rest/api/content
Any help would be appreciated! 😊