Hi!
I have the need to update 400+ confluence pages (if changed) every night to keep our server documentation up-to-date and I was wondering if there is any way to embed a few labels in the documents before publishing them so I can update each document in one fell swoop?
Or, worst case, if I want to update the document labels for these documents, do I for every document have to…
- Update the contents
- Get all current labels
- Remove labels i no longer want
- Add new labels that are missing?
I’m doing this from a Python script, so I would be greatful for any tips on the most efficient way to accomplish this pythonically :-).
Kind regards,
Goran
Holla, since you’re working with a large number of Confluence pages, automation is definitely the way to go. Luckily, the Confluence REST API provides a streamlined approach to updating both content and labels efficiently.
If you want to embed labels before publishing, you can include them in the metadata
section of your content update request. However, if your workflow involves fetching existing pages and updating them dynamically, you’ll need a slightly different approach.
And since you’re working with Python, here’s a structured way to handle updates efficiently:
- Fetch the page content (to check if it has changed).
- Compare with your new content (skip updates if unchanged).
- Retrieve existing labels using the
/rest/api/content/{page_id}/label
endpoint.
- Determine label changes (remove outdated ones, add new ones).
- Update content & labels in a single workflow to minimize API calls.
Have i answered you?
Thanks for the reply! Well, I generate the contents from Puppet, and what I really want to do is just overwrite the existing page with whatever my new contents is, including if possible labels. I don’t really care what the current contents is. I’m using the python API (Confluence module — Atlassian Python API 3.41.19 documentation), so it would be easiest for me if I can just embed the labels I want and overwrite the current page and labels - if possible, that is.
That makes sense—since your content is generated from Puppet, the best approach is to overwrite the page entirely, including its labels, in one step. Using the atlassian-python-api, you can update the page with update_page()
, remove all existing labels, and then set new ones using set_page_label()
. This keeps the process clean and efficient without unnecessary API calls. Here’s a quick implementation: update the page content, fetch and clear old labels, then add the new ones. This ensures your documentation stays up-to-date without tracking previous changes. Let me know if you need any help with the python script
Ok, so, you’re saying it needs to be a 4 step process then?
Update the page: confluence.update_page(…)
Get current labels: confluence.get_page_labels(…)
Remove all labels: confluence.remove_page_label(page_id, label)
Set new labels: confluence.set_page_label(page_id, label)
When i did this to our mediawiki server I just put the labels inside the page, updated, and that was it… But, you just can’t do something like that with confluence, then, right?
I ended up writing a function for this:
def update_page_labels(confluence, page_id, desired_labels):
"""Update labels for a confluence page to only be what I specify"""
# Get current labels from the 'results' key
current_labels = confluence.get_page_labels(page_id)['results']
# Extract current label names
current_label_names = {label['name'] for label in current_labels}
desired_label_set = set(desired_labels)
# Remove labels that aren't in the desired set
labels_to_remove = current_label_names - desired_label_set
for label in labels_to_remove:
confluence.remove_page_label(page_id, label)
# Add new labels that aren't currently present
labels_to_add = desired_label_set - current_label_names
for label in labels_to_add:
confluence.set_page_label(page_id, label)