Using Python API's To Create And Update Confluence Page

Nowadays Python is becoming very popular because it contains a wide variety of API’s that can be used to achieve any kind of functionality. On the other hand, Atlassian is becoming very popular because it is open source and very widely used.
 
In this article, we will discuss how two Python API's (Atlassian and keyring) can be used to create and update Confluence pages. This use case is very important when Python is used to execute test cases and in the end, it is required to publish test case results on confluence.
 
Atlassian API is used to update (publish) content on the confluence page and it can be downloaded from here, whereas keyring API's come default with Python and is used to get the password of the logged-in system. 
  1. Install Python on the system and map the pyton/pip folders to system variables.
  2. Clone git hub project: https://github.com/atlassian-api/atlassian-python-api
  3. Install Atlassian-api using following command: pip install Atlassian-python-API
  4. Install python keyring module using pip install keyring
  5. Create a keyring string using the following steps:
  • Under Manage User Credentials (from Control Panel)
  • Add a new generic credential.
  • Give it a meaningful name to form keypair so that it can be accessed from Python.
  • Add windows username and password.
 
 
Under the Python file, where you want to create and update the confluence page, put the below lines of code:
  1. from atlassian import Confluence  
  2. import keyring  
  3.   
  4. confluence = Confluence(  
  5. url='<url of confluence page>',  
  6. username='<username>'# of system where this page would open  
  7. password=keyring.get_password(<keyringname>, <username)) # keyring name is what you have created in step 5  
  8.   
  9. # Below would create Confluence Page  
  10. status = confluence.create_page(  
  11. space='<Confluence Space (where you want to create page)>',  
  12. title='<Page Title>',  
  13. body='This is the body. You can use <strong>HTML tags</strong>!'# Here you can put the content by using HTML tags.  
  14.   
  15. print(status)  
  16. # Below would update Confluence Page  
  17. status = confluence.update_page(  
  18. parent_id=None,  
  19. page_id=confluence.get_page_by_title(space='<Confluence Space>', title='<Page Title>'# These are names that you have used while creating page  
  20. body='Updated Page. You can use <strong>HTML tags</strong>!'# Here you can put the content by using HTML tags.  
  21. print(status)  


Next Recommended Reading How to Create Magic 8-ball in Python