For team management, we have a Confluence space to do weekly reporting. In that space, usually, one page is for a week and each page has the same template. So to copy these pages becomes a very boring work. No worries, there are Confluence APIs to help.
Installation and Connection to Confluence
Very simple and pip will help to finish everything.
1 |
pip install atlassian-python-api |
After installation, use the following code to connect to Confluence.
1 2 3 4 5 6 7 |
from atlassian import Confluence confluence = Confluence( url='http://localhost:8090', username='your_user_name', password='your_passwd', verify_ssl=False) # in my case, I ignored the SSL verification |
Copy and Create New Page
Before creating new page, you need to get the body content of the template page.
1 2 3 4 5 6 7 8 |
# retrieve the page id. page_id = confluence.get_page_id("space_a", "template page name") # retrieve the page, including its content. page = confluence.get_page_by_id(page_id, expand='body.storage') # the body HTML. print(page['body']['storage']['value']) |
Pay attention to the expand=’body.storage’. Without it, the returned page only contains the basic information, no body HTML.
Now you have the body and the rest work is a just for-loop.
1 2 3 4 5 6 |
for i in range(1, 53): print('Creating page for ww{}'.format(i)) # use the same above to retrieve the parent page's id. confluence.create_page('', '2019 WW({})'.format(i), content, parent_id=34511303) time.sleep(1) |
Also you can use confluence.update_page() to update some existing pages.