[Python] Download Confluence Pages as PDFs via API using Python

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    I’m trying to download Confluence pages in a space as PDFs using the Confluence API and a Python script. However, I am unable to download the PDF despite manually being able to export pages via the browser.

    Goal: Download all pages in a Confluence space (in a hierarchy) as PDF files using Python and an API key.

    What I’ve Tried: Basic Authentication via API I used the following Python script to send a GET request to the PDF export endpoint:

    import requests
    from requests.auth import HTTPBasicAuth

    # Configuration
    base_url = "https://abc-services.atlassian.net/wiki"
    space_key = "SPACE"
    username = "qwerty.abcd@xyz.com"
    api_token = "YOUR_API_TOKEN" # replace with your actual API token
    page_id = "463***765"

    # Create the URL for PDF export
    url = f"{base_url}/spaces/{space_key}/pdfpageexport.action?pageId={page_id}"

    # Headers
    headers = {
    "X-Atlassian-Token": "no-check"
    }

    # Send the request using basic authentication
    response = requests.get(url, auth=HTTPBasicAuth(username, api_token), headers=headers)

    # Check if the request was successful
    if response.status_code == 200:
    print("Request was successful!")
    # Save the file (optional)
    with open('page_export.pdf', 'wb') as file:
    file.write(response.content)
    else:
    print(f"Request failed with status code: {response.status_code}")
    print(response.text)


    The Issue: I am unable to download the PDF using Python, even though the browser-based export works fine. I am unsure whether I'm missing some required headers I would like to automate this process to download all pages from a Confluence space in a hierarchical structure

    Continue reading...

Compartilhe esta Página