Export and Pack Module¶
This document explains the export and pack modules that form the public API for exporting Confluence pages to XML files for AI knowledge base ingestion.
Module Overview¶
The export functionality is built in two layers:
exporter.py - Low-level export utilities
pack.py - High-level public API
exporter.py - Export Utilities¶
Location: docpack_confluence.exporter
This module provides low-level functions for exporting pages to files:
export_pages_to_xml_files()
Exports a list of Page objects to individual
XML files. Each file is named using the page’s breadcrumb path (ID or title based).
from docpack_confluence.exporter import export_pages_to_xml_files
export_pages_to_xml_files(
pages=pages, # List of Page objects
dir_out=Path("./out"), # Output directory
breadcrumb_type=BreadCrumbTypeEnum.title, # Filename format
clean_output_dir=True, # Remove dir before export
)
merge_files()
Merges all exported XML files into a single “all-in-one” document for AI context ingestion:
from docpack_confluence.exporter import merge_files
merge_files(
dir_in_list=[Path("./space1"), Path("./space2")],
path_out=Path("./all_in_one.xml"),
ext=".xml",
)
pack.py - Public API¶
Location: docpack_confluence.pack
This module provides the high-level API for end-to-end export operations.
SpaceExportConfig
Configuration for exporting pages from a single Confluence space:
from docpack_confluence.pack import SpaceExportConfig
config = SpaceExportConfig(
client=confluence_client,
space_id=12345, # or space_key="DEMO"
include=[ # URL patterns to include
"https://example.atlassian.net/wiki/.../pages/111/**",
],
exclude=[ # URL patterns to exclude
"https://example.atlassian.net/wiki/.../pages/222/*",
],
breadcrumb_type=BreadCrumbTypeEnum.title,
ignore_to_markdown_error=True,
)
ExportSpec
Specification for exporting from multiple spaces at once:
from docpack_confluence.pack import ExportSpec, SpaceExportConfig
spec = ExportSpec(
space_configs=[
SpaceExportConfig(client=client, space_id=111, include=[...]),
SpaceExportConfig(client=client, space_id=222, include=[...]),
],
dir_out=Path("./output"),
)
# Execute the export
spec.export()
# Output structure:
# ./output/
# space_id_111/
# Page A ~ Page B.xml
# Page A ~ Page C.xml
# space_id_222/
# Page X ~ Page Y.xml
# all_in_one_knowledge_base.txt # Merged file
Export Workflow¶
The export workflow consists of these steps:
Crawl: Fetch page hierarchy using
crawl_descendants()Filter: Apply include/exclude patterns using
filter_entities()Fetch Content: Get full page content using
get_pages_by_ids()Build Pages: Create
Pageobjects with entity + contentExport XML: Write individual XML files with metadata and markdown content
Merge: Combine all XML files into a single knowledge base file
Output Format¶
Each exported XML file contains:
<confluence_page>
<source_type>confluence</source_type>
<confluence_url>https://example.atlassian.net/wiki/...</confluence_url>
<title>Page Title</title>
<markdown_content>
# Page Title
Converted markdown content...
</markdown_content>
</confluence_page>
The all_in_one_knowledge_base.txt file concatenates all XML files for
easy ingestion into AI platforms.
Manual Testing with test_pack.py¶
Location: tests_manual/test_pack.py
This file provides a manual test for the export functionality using a real Confluence instance. This is NOT a unit test - it requires actual API access and is run manually during development.
Running the Test
# Run manually (not via pytest)
.venv/bin/python tests_manual/test_pack.py
Test Structure
from docpack_confluence.pack import SpaceExportConfig, ExportSpec
from docpack_confluence.tests.client import client
from docpack_confluence.tests.data import space_id
spec = ExportSpec(
space_configs=[
SpaceExportConfig(
client=client,
space_id=space_id,
include=[".../**"], # Include patterns
exclude=[".../*"], # Exclude patterns
),
],
dir_out=Path("./tmp"),
)
spec.export()
What to Verify
After running the test, check:
Output directory structure is correct
Individual XML files have proper naming (breadcrumb path)
XML content includes all expected fields
all_in_one_knowledge_base.txtcontains merged contentInclude/exclude patterns worked correctly
Development Workflow¶
When developing or debugging the export functionality:
Setup Test Data: Use
tests_manual/test.pyto create test hierarchyRun Export: Execute
tests_manual/test_pack.pyInspect Output: Check
tests_manual/tmp/directoryIterate: Modify code and re-run as needed
Note
The test uses the same 77-node test hierarchy (12 levels) used for crawler testing. See Testing Strategy and Workflow for details on the test data structure.