Python code to Update Confluence page Programmatically
The Confluence REST API allows developers to programmatically access and manipulate Confluence content and data. This can be useful for a variety of tasks such as:
- Automating the creation and update of Confluence pages and content
- Integrating Confluence with other systems and applications
- Building custom Confluence add-ons and plugins
- Extracting data from Confluence for reporting and analysis
- Synchronizing Confluence data with external databases or data sources
Using the Confluence REST API, developers can create scripts and applications that can perform actions such as creating new pages, updating existing pages, searching for content, and managing attachments. Additionally, the Confluence REST API allows developers to access the Confluence data in a structured format, making it easy to integrate Confluence with other systems and applications.
Overall, the Confluence REST API allows developers to automate and customize their workflow, increase productivity and streamline the way they work with Confluence.
Here is an example of Python code that uses the Confluence REST API to update a page in Confluence:
import requests
# Confluence API endpoint and credentials
CONFLUENCE_URL = "https://your-confluence-instance.com/rest/api"
CONFLUENCE_USERNAME = "your-username"
CONFLUENCE_PASSWORD = "your-password"
# ID of the page to update
PAGE_ID = 12345
# New content for the page
NEW_CONTENT = """
<p>This is the updated content for the page.</p>
"""
# Headers for the API request
headers = {
"Content-Type": "application/json"
}
# Data to send in the API request
data = {
"id": PAGE_ID,
"type": "page",
"title": "My Page",
"body": {
"storage": {
"value": NEW_CONTENT,
"representation": "storage"
}
}
}
# Make the API request to update the page
response = requests.put(
f"{CONFLUENCE_URL}/content/{PAGE_ID}",
auth=(CONFLUENCE_USERNAME, CONFLUENCE_PASSWORD),
headers=headers,
json=data
)
# Check the response status code
if response.status_code == 200:
print("Page updated successfully!")
else:
print(f"Error updating page: {response.text}")
Note that in this example, you need to replace the following placeholders:
CONFLUENCE_URL: should be the URL of your Confluence instance
CONFLUENCE_PASSWORD: should be the password of the Confluence user
PAGE_ID: should be the ID of the Confluence page you want to update
NEW_CONTENT: should be the new content you want to add to the page
Also, you should have requests library installed in your python environment.
You can schedule this code to automatically update the confluence page with details like infrastructure inventory , backup details etc. that should be updated regularly without any human intervention.