Inject pandas DataFrames into Excel templates with table resizing and pivot refresh.
Project description
pivoteer
pivoteer injects pandas DataFrames into existing Excel templates by editing the underlying XML. It resizes Excel Tables (ListObjects) and forces PivotTables to refresh on open without corrupting pivot caches.
Why pivoteer
Most Python Excel libraries rewrite workbooks, which can break PivotTables, filters, and formatting in real-world templates. pivoteer is designed for enterprise reporting workflows where templates are authored in Excel and must remain intact. It surgically updates only the table data and table metadata so PivotTables remain connected and refresh correctly.
Installation
pip install pivoteer
Quick Start
from pathlib import Path
import pandas as pd
from pivoteer.core import Pivoteer
pivoteer = Pivoteer(Path("template.xlsx"))
df = pd.DataFrame(
{
"Category": ["Hardware", "Software"],
"Region": ["North", "South"],
"Amount": [120.0, 250.0],
"Date": ["2024-01-01", "2024-01-02"],
}
)
pivoteer.apply_dataframe("DataSource", df)
pivoteer.save("report_output.xlsx")
Architecture Overview
- Input/output:
.xlsxfiles are ZIP archives containing OpenXML parts. - Data injection: updates
xl/worksheets/sheetN.xmlrow data using inline strings to avoid touching sharedStrings.xml. - Table resizing: updates
xl/tables/tableN.xmlby recalculating therefrange based on the DataFrame shape. - Pivot refresh: sets
refreshOnLoad="1"inxl/pivotCache/pivotCacheDefinitionN.xmlwhen present. - Pivot cache field sync (opt-in): appends missing cache field entries for table columns so new headers appear in existing PivotTables.
Features
- Surgical Data Injection: updates worksheet XML without touching sharedStrings.
- Table Resizing: recalculates ListObject ranges to match injected data.
- Pivot Preservation: sets pivot caches to refresh on load when present.
- Optional Pivot Cache Field Sync: appends missing cache field metadata for new table columns without touching PivotTable layouts.
- Minimal IO: stream-based ZIP copy-and-replace for stability.
Pivot Cache Field Sync
When new columns are added to an Excel Table, existing PivotTables often fail to show the new fields until the PivotCache metadata is updated. pivoteer can synchronize PivotCache field definitions so new table columns appear in the PivotTable field list.
What pivoteer does:
- Syncs PivotCache field metadata for the target table.
- Appends missing cache fields so new columns are visible in the PivotTable UI.
What pivoteer does not do:
- Does not create PivotTables.
- Does not modify PivotTable layouts or filters.
- Does not touch slicers or formatting.
Usage Patterns
Multiple table updates
from pivoteer.core import Pivoteer
import pandas as pd
p = Pivoteer("template.xlsx")
p.apply_dataframe("SalesData", pd.read_csv("sales.csv"))
p.apply_dataframe("CostData", pd.read_csv("costs.csv"))
p.save("report_output.xlsx")
Opt-in pivot cache field sync
from pivoteer.core import Pivoteer
import pandas as pd
p = Pivoteer("template.xlsx", enable_pivot_field_sync=True)
p.apply_dataframe("RawData", pd.read_csv("usage.csv"))
p.save("report_output.xlsx")
This flag is optional; when it is not set, pivoteer behaves exactly as before.
Advanced usage with TemplateEngine
from pathlib import Path
import pandas as pd
from pivoteer.template_engine import TemplateEngine
engine = TemplateEngine(Path("template.xlsx"))
engine.apply_dataframe("RawData", pd.read_csv("usage.csv"))
engine.sync_pivot_cache_fields()
engine.ensure_pivot_refresh_on_load()
parts = engine.get_modified_parts()
Low-level XML access
For custom XML inspection or modification, read_xml_part reads any XML part
from an Excel archive:
import zipfile
from pivoteer.xml_engine import read_xml_part
with zipfile.ZipFile("template.xlsx", "r") as archive:
tree = read_xml_part(archive, "xl/workbook.xml")
print(tree.getroot().tag)
Supported data types
pivoteer handles the following DataFrame value types when injecting rows:
| Type | Excel representation |
|---|---|
int, float |
Numeric cell (<v>) |
str |
Inline string (<is><t>) |
datetime.date, datetime.datetime |
Inline string (ISO 8601) |
None, NaN, NaT |
Empty cell (no children) |
Large datasets
pivoteer is optimized for replacing table data without rewriting the entire workbook. It is a good fit for large tables where preserving PivotTables and filters matters more than Excel formatting for each row.
Safety Guarantees
- Opt-in only: the feature is disabled unless explicitly enabled.
- Only missing cache fields are appended.
- Existing cache field order is preserved.
- PivotTable definitions are not modified.
Limitations
- The PivotCache source must reference the named Excel Table.
- The template must already contain PivotTables and pivot caches.
- The structured table must exist and be the PivotTable cache source.
- pivoteer does not auto-refresh the Excel UI; Excel recalculates pivots on open.
Compatibility
- Python: 3.10+
- Excel: Desktop Excel (Windows/macOS) supports
refreshOnLoadfor PivotTables. - Templates: Must include Excel Tables (ListObjects) with stable names.
Troubleshooting
- "Table not found": Ensure the Excel Table name matches exactly.
- "Pivot cache not found": The template may not include a PivotTable; this is expected for synthetic templates.
- "DataFrame is empty": pivoteer refuses empty payloads to protect templates.
Support and Requests
- Bugs: open a GitHub issue using the Bug Report template.
- Feature requests: open a GitHub issue using the Feature Request template.
- Security: follow the reporting process in
SECURITY.md.
Security
If you discover a vulnerability, please read SECURITY.md for reporting
instructions.
Development
python -m venv .venv
source .venv/bin/activate
pip install -e .[dev]
pytest
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pivoteer-0.2.1.tar.gz.
File metadata
- Download URL: pivoteer-0.2.1.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11d20e48e4eefcb127219d2032f9e0d54fc98a3b2e72225a2b26a96f8b61b289
|
|
| MD5 |
7b12425c498a4c84e3ff478365561345
|
|
| BLAKE2b-256 |
318ffab6202f58254221570e92da92ec08c839bab8f455c42062f3e14260757d
|
Provenance
The following attestation bundles were made for pivoteer-0.2.1.tar.gz:
Publisher:
release.yml on flitzrrr/pivoteer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pivoteer-0.2.1.tar.gz -
Subject digest:
11d20e48e4eefcb127219d2032f9e0d54fc98a3b2e72225a2b26a96f8b61b289 - Sigstore transparency entry: 963268229
- Sigstore integration time:
-
Permalink:
flitzrrr/pivoteer@c7803c1e15f363b5fec7552bb55b7c2b77047904 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/flitzrrr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c7803c1e15f363b5fec7552bb55b7c2b77047904 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pivoteer-0.2.1-py3-none-any.whl.
File metadata
- Download URL: pivoteer-0.2.1-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39af0cd80f135f4fa486cf75eb8fc4c092b9a96aa517869e3c07be9af82e1c98
|
|
| MD5 |
25be8a35ce520ce5372e13bd4916fa21
|
|
| BLAKE2b-256 |
6d6b64a334368f045f17b19dbd49bba177738ef207168790a23d7283e03c1f17
|
Provenance
The following attestation bundles were made for pivoteer-0.2.1-py3-none-any.whl:
Publisher:
release.yml on flitzrrr/pivoteer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pivoteer-0.2.1-py3-none-any.whl -
Subject digest:
39af0cd80f135f4fa486cf75eb8fc4c092b9a96aa517869e3c07be9af82e1c98 - Sigstore transparency entry: 963268233
- Sigstore integration time:
-
Permalink:
flitzrrr/pivoteer@c7803c1e15f363b5fec7552bb55b7c2b77047904 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/flitzrrr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c7803c1e15f363b5fec7552bb55b7c2b77047904 -
Trigger Event:
push
-
Statement type: