Skip to content

Instantly share code, notes, and snippets.

@haschdl
Created September 7, 2022 20:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haschdl/1734535d2f659c3254adb3f7c3df461f to your computer and use it in GitHub Desktop.
Save haschdl/1734535d2f659c3254adb3f7c3df461f to your computer and use it in GitHub Desktop.
Updating Confluence pages with a three-like numbering (1, 1.1, etc)
from atlassian import Confluence
import configparser
import json
import re
from tabulate import tabulate
config = configparser.ConfigParser()
config.read('auth.cfg')
CONF_URL=config['Confluence']['URL']
CONF_TOKEN=config['Confluence']['TOKEN']
CONF_USERNAME=config['Confluence']['USERNAME']
# A regex to match the numbering system
# This is digit followed by a dot ("1." "2." as well as "1.2")
HEADER_REGEX=r'^^(\d\.?)+(\s)'
client = Confluence(
url=CONF_URL,
username=CONF_USERNAME,
password=CONF_TOKEN,cloud=True)
def printj(c):
print(json.dumps(c,indent=1 ))
def pageNode(page, parent_level):
node={'title':page['title'],'id':page['id'],'position':page['extensions']['position']}
node['parent_level'] = parent_level
return node
def stripNumber(text):
return re.sub(HEADER_REGEX, '', text)
def rename_children_recursively(page_id,parent_level):
pages = client.get_page_child_by_type(page_id, type='page', start=None, limit=None, expand=None)
nodes = [pageNode(p,parent_level) for p in pages]
nodes = sorted(nodes, key=lambda d: d['position'])
for i, d in enumerate(nodes, 1):
d['Seq'] = i
title_strip = stripNumber(d['title'])
prefix=""
if not d['parent_level'] is None:
prefix+=f"{d['parent_level']}."
prefix=f"{prefix}{i}"
new_title = f"{prefix}. {title_strip}"
d['new_title'] = new_title
rename_children_recursively(d['id'],parent_level=prefix)
print(tabulate(nodes,headers="keys"))
#DANGER ZONE! Renaming pages.
#Uncomment the next two lines for renaming the pages
#for node in nodes:
# client.update_page(node['id'],title=node['new_title'],minor_edit=True)
##Replace with the space id of your choice
space_id='~102392913'
root = client.get_space(space_id)['homepage']
rename_children_recursively(root['id'],None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment