import requestsimport csvimport base64# Define your Confluence Cloud API URLbase_url = ""# Replace 'your-email' and 'your-api-token' with your email address and API tokenemail = ""api_token = ""
# Create a list to store all spacesall_spaces = []# Initialize 'next' URL for paginationnext_url = base_urlwhile next_url:# Combine email and API token with a colon separatorcredentials = f"{email}:{api_token}"# Encode credentials using UTF-8 encodingcredentials_bytes = credentials.encode("utf-8")# Base64 encode the credentialscredentials_base64 = base64.b64encode(credentials_bytes).decode("utf-8")# Construct the "Authorization" header with the base64-encoded credentialsheaders = {"Authorization": f"Basic {credentials_base64}", }# Make the GET request to the next URLresponse = requests.get(next_url, headers=headers)# Check if the request was successful (status code 200)if response.status_code == 200:space_data = response.json()spaces = space_data["results"]all_spaces.extend(spaces)# Check if there are more pagesnext_url = space_data.get("next")else:print(f"Error: {response.status_code} - {response.text}")break# Create a CSV file and write the headerwith open("confluence_spaces_xom.csv", mode="w", newline="") as csv_file:fieldnames = ["Space Key", "Space Name"]writer = csv.DictWriter(csv_file, fieldnames=fieldnames)writer.writeheader()# Write space data to the CSV filefor space in all_spaces:space_key = space["key"]space_name = space["name"]writer.writerow({"Space Key": space_key, "Space Name": space_name})print("Spaces exported to 'confluence_spaces_xom.csv'")
Hello Zhinoo,Make sure that you have the latest version of the atlassian python api.
pip3 install --upgrade atlassian-python-api
Then try this code:
from atlassian import Confluenceimport csvconfluence = Confluence( url='https://yoursite.atlassian.net', username = "your_email", password = "your_token", cloud=True)spaces = confluence.get_all_spaces(start=0, limit=500, expand=None)slist = spaces['results']csv_filename = 'confluence_spaces.csv'with open(csv_filename, mode='w', newline='') as csv_file: csv_writer = csv.writer(csv_file) csv_writer.writerow(['Key', 'Name']) for s in slist: csv_writer.writerow([s['key'], s['name']])print(f'Data exported to {csv_filename}')
You can read the Atlassian Python API documentation here for more information.:https://atlassian-python-api.readthedocs.io/I hope it helps.
It looks like you're new here. Sign in or register to get started.