Python API add image to Confluence

Katie Harding August 24, 2021

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?

3 answers

1 accepted

0 votes
Answer accepted
Jonathan Bradbury
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 17, 2021

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>


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


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

 

image.png

Katie Harding October 18, 2021

This actually worked!  it was slightly different from what I had tried before.  Thank you!!

Like Jonathan Bradbury likes this
0 votes
Kelly Maurice April 15, 2024

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.
0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 26, 2021

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 one
confluence.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

Katie Harding August 26, 2021

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.

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 27, 2021

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.

Katie Harding August 27, 2021

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?

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 30, 2021

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events