pythonpython-3.xconfluenceconfluence-rest-api

Confluence through Python. Upload files


I have a Confluence page with a table, I update it by code in Python, there is a cell, where I need to put link to files.

Firstly, I put in this cell underlined rows without a link using

'<td>
    <div class="content-wrapper">
        <p>
            <ac:link>
                <ri:attachment ri:filename="File_name.docx">
                <ri:page ri:content-title="Title_name" />
                </ri:attachment>
            </ac:link>
        </p>
    </div>
</td>'.

Secondly, I upload file to the entire Confluence page using

conf.attach_file(location, name=None, content_type=None, page_id=00000000, title="Check", space=None, comment="None")

Thirdly, I checked through macros Attachment in Confluence that files are attached to the page. However, when I update page, nothing changes. So those underline grey rows are still grey, they do not become blue as a link, though the name of files, certainly, are the same.

Could you help me, please? What might be a problem?

Thank you!


Solution

  • So I've tested out the confluence xhtml code you have in your post and it definitely doesn't work. I'm confused on what you're trying to do with that code though, you do not need the <ac:link> etc. tags to see your attachments.

    Upload the attachment to confluence as you are doing with python. Then you have to get the URL to the attachment:

    all_attachments = confluence.get_attachments_from_content(page_id, start=0, limit=50, expand=None, filename=None, media_type=None)
    

    This will print out a large JSON string. Somewhere in there (I don't have time to figure out the index sorry) is the link. Links look like this: where (page id is the id of page) and filename.txt is your filename

    http://wiki:8090/download/attachments/page_id/filename.txt?version=1&amp;modificationDate=1679673853281&amp;api=v2
    

    You will see that if you update an attachment on the page, the link changes to:

    http://wiki:8090/download/attachments/page_id/filename.txt?version=2&modificationDate=1679674473211&api=v2
    

    So if you use the old link with version=1, you get the old version.

    To workaround this: you can change the URL to:

    http://wiki:8090/download/attachments/page_id/filename.txt
    

    It may be easier to just create the URLs yourself (from my experience there was no problems with this):

    url = f"http://wiki:8090/download/attachments/{page_id}/{filename}"
    

    Now that you have the link you can attach it into confluence I'm not sure what you are actually doing with these files so here is an example:

    html_link = f'<a href={url}>Press here to go to file.txt</a>'
    

    Then put the code somewhere in the confluence page with bs4:

    #Lets say you have a td with class "file_goes_here" already in your XHTML
    cell = (soup.find("td", {"class": "file_goes_here"})
    #html_link is the link we created earlier above
    cell.replace_with(html_link)
    

    Then update the confluence page with the new contents:

    confluence.update_page(page_id, title, body, parent_id=None, type='page', representation='storage', minor_edit=False, full_width=False)