Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update/delete confluence page using python code

I want to update a confluence page through the Confluence REST API. Please suggest a code snippet with which I can update the page through its "Page Title".

Assume my confluence site is https://wiki.mydomain.com, the page title is TEST and the space is TST.

like image 701
Rishi Avatar asked May 10 '17 09:05

Rishi


People also ask

How do I access confluence in Python?

First, the url needs to be the base url of your confluence site, so in your case something along the lines of url='https://conf.myorg.com' . Second, using the parameters username= and password= is for basic auth.

Can I use Python in Confluence?

@Bernd SteffensRunning python directly from SDK plugin is possible but it is not recommended as it voids all the security provided by JVM in Confluence.

Does confluence have a REST API?

Confluence's REST APIs provide access to resources (data entities) via URI paths. To use a REST API, your application will make an HTTP request and parse the response. By default, the response format is JSON. Your methods will be the standard HTTP methods: GET, PUT, POST and DELETE.


2 Answers

You can also use the Confluence module of the Atlassian Python API to update / delete Confluence pages from Python. This is basically a wrapper for the REST API to provide a simple interface.

Installation via pip:

pip install atlassian-python-api

Usage:

from atlassian import Confluence

conf_site = 'https://wiki.mydomain.com'
conf_user = 'username'
conf_pass = 'password'

page_title = 'TEST' 
page_space = 'TST'

# connect to Confluence 
conf = Confluence(url=conf_site, username=conf_user, password=conf_pass)

# resolve page ID
page_id = conf.get_page_id(page_space, page_title)

# optonal: get current page content, in case you want to base your editing on that
page = conf.get_page_by_id(page_id, expand='body.storage')
page_content = page['body']['storage']['value']

# new page content
page_content =  '<p>This is the new updated text of the page</p>'

# update page with new content
conf.update_page(page_id, page_title, page_content)

Or, to delete:

# This method removes a page, if it has recursive flag, method removes including child pages
conf.remove_page(page_id, status=None, recursive=False)
like image 117
buddemat Avatar answered Oct 13 '22 06:10

buddemat


As you can see in Atlassian Documentations (here) you can update pages via following curl:

curl -u admin:admin -X PUT -H 'Content-Type: application/json' -d'{"id":"3604482","type":"page",
"title":"new page","space":{"key":"TST"},"body":{"storage":{"value":
"<p>This is the updated text for the new page</p>","representation":"storage"}},
"version":{"number":2}}' http://localhost:8080/confluence/rest/api/content/3604482 | python -mjson.tool

However it works with Page ID rather than a page Title. You can grab the id with following:

curl -u admin:admin -X GET "http://localhost:8080/confluence/rest/api/content?title=myPage%20Title
&spaceKey=TST&expand=history" | python -mjson.tool

Just as a side note since you look like a new user, here we won't provide code snippet and you need to tell us what you have tried and what is your problem actually. I would recommend you to take a look at How do I ask a good question as well :-)

like image 1
Saleh Parsa Avatar answered Oct 13 '22 07:10

Saleh Parsa