Python API add image to Confluence
Asked by Katie Harding:

I would like to add images to confluence pages. What I am trying is this:

```
filename = 'images/conc_v_input.png'
image = '<ac:image ac:title="ConcVInput" ac:alt="concvinput" ac:height="400"><ri:attachment ri:filename="images/conc_v_input.png" ri:version-at-save="3" /></ac:image>'

confluence.append_page(my_page, my_title, image, parent_id=unit_test_analysis, 
type='page', representation='storage', minor_edit=False)
```

What I get is Unknown Attachment with the hover text of 'images/conc_v_input.png'

Any way to sort this out?


Answers:
author: Jonathan Bradbury
body: It looks like if you upload the image first, then you can reference it and show it on the page.  

```
<ac:image>

<ri:attachment ri:filename=

"tb.png" />

</ac:image>
```

<br />

This was helpful, but seems like you might already know of it. I used the images section.  
<https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html>

<br />

The file name is the name of the attachment you uploaded.

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/165313i2E576EB1DF5ECABA/image-size/large?v=v2&px=999 "image.png")

comments: author: Katie Harding
body: This actually worked! it was slightly different from what I had tried before. Thank you!!


author: Kelly Maurice
body: I've been trying for a while to make this work. I have a confluence page and can add attachments to it via python program and REST API. This is vanilla, similar to many other examples online.

The basic code that always returns 409 is:  

image_markup = f'\<ac:link ac:anchor="Anchor Link"\>\<ac:link-body\> \<ac:image\>\<ri:attachment ri:filename="sino_NEW.png"/\>\</ac:image\>\</ac:link-body\>\</ac:link\>'  
payload = { "id": page_id, "type": "page", "version": {"number": 1},  
"body": {  
"storage": {  
"value": f"\<p\>Adding image to page\</p\>{image_markup}",  
"representation": "storage"  
}  
}  
}  
headers = {  
'Content-Type': 'application/json',  
'Accept': 'application/json'  
}  
response = requests.put(page_update_url, auth=(username, password), json=payload, headers=headers)  
This is not an answer but a question, as this is the correct question and I'm going in circles trying to find the fix.


author: Andy Heinzer
body: Hi Katie,

If I understand correctly, you're using the Python API module to try to add an image attachment to an existing page. The unknown attachment would seem to indicate that Confluence doesn't have this image added yet.

Looking into what I believe is the documentation for that API over in <https://atlassian-python-api.readthedocs.io/confluence.html> I think that we need to make sure Confluence knows of this attachment first.

```
# Attach (upload) a file to a page, if it exists it will update the# automatically version the new file and keep the old oneconfluence.attach_file(filename, name=None, content_type=None, page_id=None, title=None, space=None, comment=None)
```

Try that first. This should then allow that page in Confluence to use that image.

Let me know if this helps.

Andy

comments: author: Katie Harding
body: Sorry that didn't work. It was also one of the things I had tried before. The figure doesn't appear in the page at all either. If it were attached, shouldn't it be on the page?

Thanks for trying.


author: Andy Heinzer
body: Confluence handles attachments a little differently than say Jira does. Just because an attachment is connected to a page, does not necessarily mean it appears on that page automatically. If you're looking at this page in Confluence Cloud, click the ellipsis (...) menu on the top right of the page and go to Attachments. This will show what files are attached to that page. The URL tends look like

/wiki/pages/viewpageattachments.action?pageId=600581878

Try checking this after making that attach call. I'm interested learn if perhaps some form of your image is displayed there or not first. If there is an entry for your attachment, I'd be curious to see if it displays on this page of Attachments or if the image is somehow corrupted.


author: Katie Harding
body: Thank you for the info about where the file is attached. the image is attached (I can see the file, and click on it and it is the correct one).

The image is not corrupted, and it looks perfect.

Any suggestions about the next step?


author: Andy Heinzer
body: I noticed that in my own Confluence Cloud site, that uploading a filename with a '/' character is replacing that character with a ':' instead. This could be a factor here in regards to the way you are referring to that file.

Once the file is uploaded to the page, see if you can then refer to it by the filename of

```
images:conc_v_input.png
```

instead of

```
images/conc_v_input.png
```

This is likely why Confluence does not understand the filename you are telling it to append to that page. Try that and let me know if that helps.

Andy


Tags: cloud

confluence-cloud

python