Bulk Find and Replace Confluence Page Content Using Python

Here’s a script for bulk finding and replacing content in a Confluence instance.

Because Confluence page content is stored as one big string of text, it’s pretty simple to do a find-and-replace across all pages in all spaces.

One likely candidate for such an action is finding and replacing all references to one url, with a reference to a different one.

Please read this page if you’re unsure about authenticating against the Atlassian Cloud API.

 

import requests
import base64
import json

username = "<your email address"
api_token = "<api token>"
cloud_url = "https://<confluence instance>.atlassian.net"

input_string = f'{username}:{api_token}'

input_bytes = input_string.encode('utf-8')
encoded_bytes = base64.b64encode(input_bytes)
encoded_string = encoded_bytes.decode('utf-8')

headers = {
    "Authorization" : f"Basic {encoded_string}",
    'Content-Type': 'application/json'
}

get_spaces = requests.get(f"{cloud_url}/wiki/api/v2/spaces", headers=headers)

spaces_json = json.loads(get_spaces.text)

for space in spaces_json["results"]:
    space_id = space["id"]
    get_pages = requests.get(f"{cloud_url}/wiki/api/v2/spaces/{space_id}/pages?body-format=storage", headers=headers)
    pages_json = json.loads(get_pages.text)
    for page in pages_json["results"]:

        page_version = page["version"]["number"]
        page_id = page["id"]
        page_title = page["title"]
        
        page_body = page["body"]["storage"]["value"]
        
        
        if str(page_body).__contains__("12345"):

            updated_page_body = str(page_body).replace("12345", "ABCDEF")
    
            page_payload = json.dumps({
            "id": page_id,
            "status": "current",
            "title": page_title,
            "body": {
                "representation": "storage",
                "value": updated_page_body
            },
            "version": {
                "number": int(page_version) + 1,
    
            }
            })
        
            update_page = requests.put(f"{cloud_url}/wiki/api/v2/pages/{page_id}", headers=headers, data=page_payload)
            print(update_page.text)
            print(update_page.status_code)
        

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *