XlsxWriter 3.3.2__tar.gz → 3.3.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/Changes +17 -0
- {xlsxwriter-3.3.2/xlsxwriter.egg-info → xlsxwriter-3.3.3}/PKG-INFO +3 -1
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/README.rst +2 -0
- xlsxwriter-3.3.3/examples/pivot_table.py +287 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/pyproject.toml +1 -1
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/__init__.py +1 -1
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/contenttypes.py +19 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/exceptions.py +4 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/packager.py +59 -0
- xlsxwriter-3.3.3/xlsxwriter/pivot_cache.py +165 -0
- xlsxwriter-3.3.3/xlsxwriter/pivot_table.py +809 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/workbook.py +106 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/worksheet.py +163 -1
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3/xlsxwriter.egg-info}/PKG-INFO +3 -1
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter.egg-info/SOURCES.txt +3 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/LICENSE.txt +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/MANIFEST.in +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/array_formula.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/autofilter.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/autofilter_data.txt +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/autofit.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/autofit_manually.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/background.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/cell_indentation.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_area.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_bar.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_clustered.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_column.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_combined.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_data_labels.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_data_table.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_data_tools.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_date_axis.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_doughnut.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_gauge.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_gradient.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_line.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_pareto.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_pattern.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_pie.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_radar.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_scatter.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_secondary_axis.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_stock.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chart_styles.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/chartsheet.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/check_close.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/checkbox.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/colors.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/comments1.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/comments2.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/conditional_format.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/context_manager.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/data_validate.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/datetimes.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/defined_name.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/demo.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/diagonal_border.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/django_simple.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/doc_properties.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/dynamic_arrays.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/embedded_images.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/headers_footers.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/hello_world.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/hide_row_col.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/hide_sheet.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/http_server.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/hyperlink.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/ignore_errors.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/images.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/images_bytesio.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/inheritance1.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/inheritance2.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/lambda.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/macros.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/merge1.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/merge_rich_string.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/outline.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/outline_collapsed.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_autofilter.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_chart.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_chart_columns.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_chart_line.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_column_formats.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_conditional_format.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_datetime.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_header_format.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_multiple.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_percentage.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_positioning.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_simple.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/pandas_table.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/panes.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_chart.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_checkbox.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_conditional_format.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_format_custom.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_format_default.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_multiple.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_positioning.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_simple.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_sparklines.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/polars_xlsxwriter.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/rich_strings.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/right_to_left.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/sensitivity_label.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/sparklines1.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/sparklines2.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/tab_colors.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/tables.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/text_indent.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/textbox.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/theme_custom.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/theme_excel_2023.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/tutorial1.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/tutorial2.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/tutorial3.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/unicode.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/unicode_polish_utf8.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/unicode_polish_utf8.txt +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/unicode_shift_jis.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/unicode_shift_jis.txt +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/user_types1.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/user_types2.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/user_types3.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/vbaProject.bin +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/vba_extract.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/watermark.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/examples/worksheet_protection.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/setup.cfg +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/app.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_area.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_bar.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_column.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_doughnut.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_line.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_pie.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_radar.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_scatter.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_stock.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chart_title.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/chartsheet.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/color.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/comments.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/core.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/custom.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/drawing.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/feature_property_bag.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/format.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/image.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/metadata.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/relationships.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/rich_value.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/rich_value_rel.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/rich_value_structure.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/rich_value_types.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/shape.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/sharedstrings.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/styles.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/table.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/theme.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/url.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/utility.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/vml.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter/xmlwriter.py +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter.egg-info/dependency_links.txt +0 -0
- {xlsxwriter-3.3.2 → xlsxwriter-3.3.3}/xlsxwriter.egg-info/top_level.txt +0 -0
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
|
|
2
|
+
Release 3.3.3 - November 20 2026
|
|
3
|
+
--------------------------------
|
|
4
|
+
|
|
5
|
+
* Added support for worksheet pivot tables via the :func:`add_pivot_table`
|
|
6
|
+
method. Pivot tables can have row, column, filter and value fields, and the
|
|
7
|
+
value fields can have different functions, custom names and number formats.
|
|
8
|
+
The pivot table style, layout and grand totals can also be set. See
|
|
9
|
+
:ref:`pivot_tables` and :ref:`ex_pivot_table`.
|
|
10
|
+
|
|
11
|
+
**Note**, the pivot table data isn't stored in the file. Excel builds the
|
|
12
|
+
pivot table from the source data when the file is opened. As a result,
|
|
13
|
+
applications that don't refresh pivot tables on load will show an empty
|
|
14
|
+
pivot table, and Excel will ask to save the file on close even if no
|
|
15
|
+
changes were made. See :ref:`pivot_table_limitations` for details.
|
|
16
|
+
|
|
17
|
+
:feature:`1231`.
|
|
18
|
+
|
|
2
19
|
Release 3.3.2 - November 19 2026
|
|
3
20
|
--------------------------------
|
|
4
21
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: xlsxwriter
|
|
3
|
-
Version: 3.3.
|
|
3
|
+
Version: 3.3.3
|
|
4
4
|
Summary: A Python module for creating Excel XLSX files.
|
|
5
5
|
Author-email: John McNamara <jmcnamara@cpan.org>
|
|
6
6
|
License-Expression: BSD-2-Clause
|
|
@@ -43,6 +43,8 @@ including:
|
|
|
43
43
|
* Autofilters.
|
|
44
44
|
* Data validation and drop down lists.
|
|
45
45
|
* Conditional formatting.
|
|
46
|
+
* Worksheet tables.
|
|
47
|
+
* Pivot tables.
|
|
46
48
|
* Worksheet PNG/JPEG/GIF/BMP/WMF/EMF images.
|
|
47
49
|
* Rich multi-format strings.
|
|
48
50
|
* Cell comments.
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# An example of how to create pivot tables with the XlsxWriter module.
|
|
4
|
+
#
|
|
5
|
+
# A pivot table is a way of grouping and summarizing the data in a worksheet
|
|
6
|
+
# range.
|
|
7
|
+
#
|
|
8
|
+
# SPDX-License-Identifier: BSD-2-Clause
|
|
9
|
+
#
|
|
10
|
+
# Copyright (c) 2013-2026, John McNamara, jmcnamara@cpan.org
|
|
11
|
+
#
|
|
12
|
+
import xlsxwriter
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def main():
|
|
16
|
+
workbook = xlsxwriter.Workbook("pivot_table.xlsx")
|
|
17
|
+
|
|
18
|
+
# Add a worksheet with some sample data for the pivot tables.
|
|
19
|
+
worksheet = workbook.add_worksheet("Data")
|
|
20
|
+
populate_pivot_data(workbook, worksheet)
|
|
21
|
+
|
|
22
|
+
# -------------------------------------------------------------------------
|
|
23
|
+
# 1. Add an empty pivot table.
|
|
24
|
+
# -------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
# Add a pivot table based on the worksheet data to a new worksheet.
|
|
27
|
+
worksheet = workbook.add_worksheet("Pivot1")
|
|
28
|
+
worksheet.add_pivot_table("A3", {"source": "Data!A1:D51"})
|
|
29
|
+
|
|
30
|
+
# -------------------------------------------------------------------------
|
|
31
|
+
# 2. Add a pivot table with a Row field and a Value field.
|
|
32
|
+
# -------------------------------------------------------------------------
|
|
33
|
+
|
|
34
|
+
# Add a pivot table that summarizes the "Volume" data by "Region". Note
|
|
35
|
+
# that the data source can also be specified as a list.
|
|
36
|
+
worksheet = workbook.add_worksheet("Pivot2")
|
|
37
|
+
worksheet.add_pivot_table(
|
|
38
|
+
2,
|
|
39
|
+
0,
|
|
40
|
+
{
|
|
41
|
+
"source": ["Data", 0, 0, 50, 3],
|
|
42
|
+
"rows": ["Region"],
|
|
43
|
+
"values": [{"field": "Volume"}],
|
|
44
|
+
},
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# -------------------------------------------------------------------------
|
|
48
|
+
# 3. Add a pivot table with a Row field, a Column field and a Value field.
|
|
49
|
+
# -------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
# Add a pivot table that summarizes the "Volume" data by "Region" and
|
|
52
|
+
# "Item".
|
|
53
|
+
worksheet = workbook.add_worksheet("Pivot3")
|
|
54
|
+
worksheet.add_pivot_table(
|
|
55
|
+
"A3",
|
|
56
|
+
{
|
|
57
|
+
"source": "Data!A1:D51",
|
|
58
|
+
"rows": ["Region"],
|
|
59
|
+
"columns": ["Item"],
|
|
60
|
+
"values": [{"field": "Volume"}],
|
|
61
|
+
},
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
# -------------------------------------------------------------------------
|
|
65
|
+
# 4. Add a pivot table with a Filter field.
|
|
66
|
+
# -------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
# Add a pivot table that summarizes the "Volume" data by "Region", with a
|
|
69
|
+
# filter on "Month". The filter is displayed in the first row and the
|
|
70
|
+
# pivot table body starts in the third row.
|
|
71
|
+
worksheet = workbook.add_worksheet("Pivot4")
|
|
72
|
+
worksheet.add_pivot_table(
|
|
73
|
+
"A1",
|
|
74
|
+
{
|
|
75
|
+
"source": "Data!A1:D51",
|
|
76
|
+
"rows": ["Region"],
|
|
77
|
+
"values": [{"field": "Volume"}],
|
|
78
|
+
"filters": ["Month"],
|
|
79
|
+
},
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
# -------------------------------------------------------------------------
|
|
83
|
+
# 5. Add a pivot table with two Row fields.
|
|
84
|
+
# -------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
# Add a pivot table that summarizes the "Volume" data by "Region" and then
|
|
87
|
+
# by "Item" within each region.
|
|
88
|
+
worksheet = workbook.add_worksheet("Pivot5")
|
|
89
|
+
worksheet.add_pivot_table(
|
|
90
|
+
"A3",
|
|
91
|
+
{
|
|
92
|
+
"source": "Data!A1:D51",
|
|
93
|
+
"rows": ["Region", "Item"],
|
|
94
|
+
"values": [{"field": "Volume"}],
|
|
95
|
+
},
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# -------------------------------------------------------------------------
|
|
99
|
+
# 6. Add a pivot table with several Value fields.
|
|
100
|
+
# -------------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
# Add a pivot table that summarizes the "Volume" data by "Region" using
|
|
103
|
+
# different functions.
|
|
104
|
+
worksheet = workbook.add_worksheet("Pivot6")
|
|
105
|
+
worksheet.add_pivot_table(
|
|
106
|
+
"A3",
|
|
107
|
+
{
|
|
108
|
+
"source": "Data!A1:D51",
|
|
109
|
+
"rows": ["Region"],
|
|
110
|
+
"values": [
|
|
111
|
+
{"field": "Volume"},
|
|
112
|
+
{"field": "Volume", "function": "count"},
|
|
113
|
+
{"field": "Volume", "function": "average"},
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# -------------------------------------------------------------------------
|
|
119
|
+
# 7. Add a pivot table with a custom name and number format.
|
|
120
|
+
# -------------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
# Add a pivot table that summarizes the "Volume" data by "Region", with a
|
|
123
|
+
# custom name and number format.
|
|
124
|
+
worksheet = workbook.add_worksheet("Pivot7")
|
|
125
|
+
worksheet.add_pivot_table(
|
|
126
|
+
"A3",
|
|
127
|
+
{
|
|
128
|
+
"source": "Data!A1:D51",
|
|
129
|
+
"rows": ["Region"],
|
|
130
|
+
"values": [
|
|
131
|
+
{"field": "Volume", "name": "Total Volume", "num_format": "#,##0"},
|
|
132
|
+
],
|
|
133
|
+
},
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
# -------------------------------------------------------------------------
|
|
137
|
+
# 8. Add a pivot table with a different style and no grand totals.
|
|
138
|
+
# -------------------------------------------------------------------------
|
|
139
|
+
|
|
140
|
+
# Add a pivot table that summarizes the "Volume" data by "Region" and
|
|
141
|
+
# "Item", without grand totals.
|
|
142
|
+
worksheet = workbook.add_worksheet("Pivot8")
|
|
143
|
+
worksheet.add_pivot_table(
|
|
144
|
+
"A3",
|
|
145
|
+
{
|
|
146
|
+
"source": "Data!A1:D51",
|
|
147
|
+
"rows": ["Region"],
|
|
148
|
+
"columns": ["Item"],
|
|
149
|
+
"values": [{"field": "Volume"}],
|
|
150
|
+
"style": "Pivot Style Medium 9",
|
|
151
|
+
"row_grand_totals": False,
|
|
152
|
+
"column_grand_totals": False,
|
|
153
|
+
},
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
# -------------------------------------------------------------------------
|
|
157
|
+
# 9. Add a pivot table with a tabular layout.
|
|
158
|
+
# -------------------------------------------------------------------------
|
|
159
|
+
|
|
160
|
+
# Add a pivot table that summarizes the "Volume" data by "Region" and
|
|
161
|
+
# "Item", with each row field in its own column.
|
|
162
|
+
worksheet = workbook.add_worksheet("Pivot9")
|
|
163
|
+
worksheet.add_pivot_table(
|
|
164
|
+
"A3",
|
|
165
|
+
{
|
|
166
|
+
"source": "Data!A1:D51",
|
|
167
|
+
"rows": ["Region", "Item"],
|
|
168
|
+
"values": [{"field": "Volume"}],
|
|
169
|
+
"layout": "tabular",
|
|
170
|
+
},
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
# -------------------------------------------------------------------------
|
|
174
|
+
# 10. Add two pivot tables to the same worksheet.
|
|
175
|
+
# -------------------------------------------------------------------------
|
|
176
|
+
|
|
177
|
+
# Add two pivot tables side by side that summarize the "Volume" data by
|
|
178
|
+
# "Region" in one and by "Item" in the other.
|
|
179
|
+
worksheet = workbook.add_worksheet("Pivot10")
|
|
180
|
+
worksheet.add_pivot_table(
|
|
181
|
+
"A3",
|
|
182
|
+
{
|
|
183
|
+
"source": "Data!A1:D51",
|
|
184
|
+
"rows": ["Region"],
|
|
185
|
+
"values": [{"field": "Volume"}],
|
|
186
|
+
},
|
|
187
|
+
)
|
|
188
|
+
worksheet.add_pivot_table(
|
|
189
|
+
"F3",
|
|
190
|
+
{
|
|
191
|
+
"source": "Data!A1:D51",
|
|
192
|
+
"rows": ["Item"],
|
|
193
|
+
"values": [{"field": "Volume"}],
|
|
194
|
+
},
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
# -------------------------------------------------------------------------
|
|
198
|
+
# 11. Add a pivot table to the same worksheet as the data.
|
|
199
|
+
# -------------------------------------------------------------------------
|
|
200
|
+
|
|
201
|
+
# Add a new worksheet with a copy of the sample data.
|
|
202
|
+
worksheet = workbook.add_worksheet("Pivot11")
|
|
203
|
+
populate_pivot_data(workbook, worksheet)
|
|
204
|
+
|
|
205
|
+
# Add a pivot table to the right of the data that summarizes the "Volume"
|
|
206
|
+
# data by "Region".
|
|
207
|
+
worksheet.add_pivot_table(
|
|
208
|
+
"G3",
|
|
209
|
+
{
|
|
210
|
+
"source": "Pivot11!A1:D51",
|
|
211
|
+
"rows": ["Region"],
|
|
212
|
+
"values": [{"field": "Volume"}],
|
|
213
|
+
},
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
workbook.close()
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def populate_pivot_data(workbook, worksheet):
|
|
220
|
+
# Add the sample data for the pivot tables to the worksheet.
|
|
221
|
+
data = [
|
|
222
|
+
["East", "Apple", 9000, "July"],
|
|
223
|
+
["East", "Apple", 5000, "April"],
|
|
224
|
+
["South", "Orange", 9000, "September"],
|
|
225
|
+
["North", "Apple", 2000, "November"],
|
|
226
|
+
["West", "Apple", 9000, "November"],
|
|
227
|
+
["South", "Pear", 7000, "October"],
|
|
228
|
+
["North", "Pear", 9000, "August"],
|
|
229
|
+
["West", "Orange", 1000, "December"],
|
|
230
|
+
["West", "Grape", 1000, "November"],
|
|
231
|
+
["South", "Pear", 10000, "April"],
|
|
232
|
+
["West", "Grape", 6000, "January"],
|
|
233
|
+
["South", "Orange", 3000, "May"],
|
|
234
|
+
["North", "Apple", 3000, "December"],
|
|
235
|
+
["South", "Apple", 7000, "February"],
|
|
236
|
+
["West", "Grape", 1000, "December"],
|
|
237
|
+
["East", "Grape", 8000, "February"],
|
|
238
|
+
["South", "Grape", 10000, "June"],
|
|
239
|
+
["West", "Pear", 7000, "December"],
|
|
240
|
+
["South", "Apple", 2000, "October"],
|
|
241
|
+
["East", "Grape", 7000, "December"],
|
|
242
|
+
["North", "Grape", 6000, "July"],
|
|
243
|
+
["East", "Pear", 8000, "February"],
|
|
244
|
+
["North", "Apple", 7000, "August"],
|
|
245
|
+
["North", "Orange", 7000, "July"],
|
|
246
|
+
["North", "Apple", 6000, "June"],
|
|
247
|
+
["South", "Grape", 8000, "September"],
|
|
248
|
+
["West", "Apple", 3000, "October"],
|
|
249
|
+
["South", "Orange", 10000, "November"],
|
|
250
|
+
["West", "Grape", 4000, "December"],
|
|
251
|
+
["North", "Orange", 5000, "August"],
|
|
252
|
+
["East", "Orange", 1000, "November"],
|
|
253
|
+
["East", "Orange", 4000, "October"],
|
|
254
|
+
["North", "Grape", 5000, "August"],
|
|
255
|
+
["East", "Apple", 1000, "July"],
|
|
256
|
+
["South", "Apple", 10000, "March"],
|
|
257
|
+
["East", "Grape", 7000, "October"],
|
|
258
|
+
["West", "Grape", 1000, "September"],
|
|
259
|
+
["East", "Grape", 10000, "October"],
|
|
260
|
+
["South", "Orange", 8000, "March"],
|
|
261
|
+
["North", "Apple", 4000, "July"],
|
|
262
|
+
["South", "Orange", 5000, "July"],
|
|
263
|
+
["West", "Apple", 4000, "June"],
|
|
264
|
+
["East", "Apple", 5000, "April"],
|
|
265
|
+
["North", "Pear", 3000, "August"],
|
|
266
|
+
["East", "Grape", 9000, "November"],
|
|
267
|
+
["North", "Orange", 8000, "October"],
|
|
268
|
+
["East", "Apple", 10000, "June"],
|
|
269
|
+
["South", "Pear", 1000, "December"],
|
|
270
|
+
["North", "Grape", 10000, "July"],
|
|
271
|
+
["East", "Grape", 6000, "February"],
|
|
272
|
+
]
|
|
273
|
+
|
|
274
|
+
# Widen the columns for clarity.
|
|
275
|
+
worksheet.set_column(0, 3, 12)
|
|
276
|
+
|
|
277
|
+
# Write the header titles.
|
|
278
|
+
header_format = workbook.add_format({"bold": True})
|
|
279
|
+
worksheet.write_row(0, 0, ["Region", "Item", "Volume", "Month"], header_format)
|
|
280
|
+
|
|
281
|
+
# Write the other worksheet data.
|
|
282
|
+
for row, row_data in enumerate(data, 1):
|
|
283
|
+
worksheet.write_row(row, 0, row_data)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
if __name__ == "__main__":
|
|
287
|
+
main()
|
|
@@ -150,6 +150,25 @@ class ContentTypes(xmlwriter.XMLwriter):
|
|
|
150
150
|
|
|
151
151
|
self._add_override((table_name, APP_DOCUMENT + "spreadsheetml.table+xml"))
|
|
152
152
|
|
|
153
|
+
def _add_pivot_table_name(self, pivot_table_name: str) -> None:
|
|
154
|
+
# Add the name of a pivot table to the ContentTypes overrides.
|
|
155
|
+
pivot_table_name = "/xl/pivotTables/" + pivot_table_name + ".xml"
|
|
156
|
+
|
|
157
|
+
self._add_override(
|
|
158
|
+
(pivot_table_name, APP_DOCUMENT + "spreadsheetml.pivotTable+xml")
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
def _add_pivot_cache_name(self, pivot_cache_name: str) -> None:
|
|
162
|
+
# Add the name of a pivot cache to the ContentTypes overrides.
|
|
163
|
+
pivot_cache_name = "/xl/pivotCache/" + pivot_cache_name + ".xml"
|
|
164
|
+
|
|
165
|
+
self._add_override(
|
|
166
|
+
(
|
|
167
|
+
pivot_cache_name,
|
|
168
|
+
APP_DOCUMENT + "spreadsheetml.pivotCacheDefinition+xml",
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
|
|
153
172
|
def _add_vba_project(self) -> None:
|
|
154
173
|
# Add a vbaProject to the ContentTypes defaults.
|
|
155
174
|
|
|
@@ -40,6 +40,10 @@ class OverlappingRange(XlsxInputError):
|
|
|
40
40
|
"""Worksheet merge range or table overlaps previous range."""
|
|
41
41
|
|
|
42
42
|
|
|
43
|
+
class PivotTableError(XlsxInputError):
|
|
44
|
+
"""Pivot table options or source data are invalid."""
|
|
45
|
+
|
|
46
|
+
|
|
43
47
|
class UndefinedImageSize(XlsxFileError):
|
|
44
48
|
"""No size data found in image file."""
|
|
45
49
|
|
|
@@ -23,6 +23,7 @@ from xlsxwriter.custom import Custom
|
|
|
23
23
|
from xlsxwriter.exceptions import EmptyChartSeries
|
|
24
24
|
from xlsxwriter.feature_property_bag import FeaturePropertyBag
|
|
25
25
|
from xlsxwriter.metadata import Metadata
|
|
26
|
+
from xlsxwriter.pivot_table import PivotTable
|
|
26
27
|
from xlsxwriter.relationships import Relationships
|
|
27
28
|
from xlsxwriter.rich_value import RichValue
|
|
28
29
|
from xlsxwriter.rich_value_rel import RichValueRel
|
|
@@ -99,6 +100,8 @@ class Packager:
|
|
|
99
100
|
self.chart_count = 0
|
|
100
101
|
self.drawing_count = 0
|
|
101
102
|
self.table_count = 0
|
|
103
|
+
self.pivot_table_count = 0
|
|
104
|
+
self.pivot_cache_count = 0
|
|
102
105
|
self.num_vml_files = 0
|
|
103
106
|
self.num_comment_files = 0
|
|
104
107
|
self.named_ranges = []
|
|
@@ -126,6 +129,7 @@ class Packager:
|
|
|
126
129
|
self.num_vml_files = workbook.num_vml_files
|
|
127
130
|
self.num_comment_files = workbook.num_comment_files
|
|
128
131
|
self.named_ranges = workbook.named_ranges
|
|
132
|
+
self.pivot_cache_count = len(workbook.pivot_caches)
|
|
129
133
|
|
|
130
134
|
for worksheet in self.workbook.worksheets():
|
|
131
135
|
if worksheet.is_chartsheet:
|
|
@@ -133,6 +137,8 @@ class Packager:
|
|
|
133
137
|
else:
|
|
134
138
|
self.worksheet_count += 1
|
|
135
139
|
|
|
140
|
+
self.pivot_table_count += len(worksheet.pivot_tables)
|
|
141
|
+
|
|
136
142
|
def _create_package(self):
|
|
137
143
|
# Write the xml files that make up the XLSX OPC package.
|
|
138
144
|
self._write_content_types_file()
|
|
@@ -146,6 +152,8 @@ class Packager:
|
|
|
146
152
|
self._write_vml_files()
|
|
147
153
|
self._write_comment_files()
|
|
148
154
|
self._write_table_files()
|
|
155
|
+
self._write_pivot_table_files()
|
|
156
|
+
self._write_pivot_cache_files()
|
|
149
157
|
self._write_shared_strings_file()
|
|
150
158
|
self._write_styles_file()
|
|
151
159
|
self._write_custom_file()
|
|
@@ -467,6 +475,12 @@ class Packager:
|
|
|
467
475
|
for i in range(1, self.table_count + 1):
|
|
468
476
|
content._add_table_name("table" + str(i))
|
|
469
477
|
|
|
478
|
+
for i in range(1, self.pivot_table_count + 1):
|
|
479
|
+
content._add_pivot_table_name("pivotTable" + str(i))
|
|
480
|
+
|
|
481
|
+
for i in range(1, self.pivot_cache_count + 1):
|
|
482
|
+
content._add_pivot_cache_name("pivotCacheDefinition" + str(i))
|
|
483
|
+
|
|
470
484
|
for i in range(1, self.num_comment_files + 1):
|
|
471
485
|
content._add_comment_name("comments" + str(i))
|
|
472
486
|
|
|
@@ -554,6 +568,45 @@ class Packager:
|
|
|
554
568
|
table._assemble_xml_file()
|
|
555
569
|
index += 1
|
|
556
570
|
|
|
571
|
+
def _write_pivot_table_files(self) -> None:
|
|
572
|
+
# Write the pivot table files and their rels files.
|
|
573
|
+
index = 1
|
|
574
|
+
for worksheet in self.workbook.worksheets():
|
|
575
|
+
for pivot_table in worksheet.pivot_tables:
|
|
576
|
+
pivot_table._set_xml_writer(
|
|
577
|
+
self._filename(f"xl/pivotTables/pivotTable{index}.xml")
|
|
578
|
+
)
|
|
579
|
+
pivot_table._set_num_format_indices(self.workbook.xf_formats)
|
|
580
|
+
pivot_table._assemble_xml_file()
|
|
581
|
+
|
|
582
|
+
# Write the rels file that links the pivot table to its cache.
|
|
583
|
+
self._write_pivot_table_rels_file(pivot_table, index)
|
|
584
|
+
|
|
585
|
+
index += 1
|
|
586
|
+
|
|
587
|
+
def _write_pivot_table_rels_file(self, pivot_table: PivotTable, index: int) -> None:
|
|
588
|
+
# Write the pivot table .rels file, such as
|
|
589
|
+
# xl/pivotTables/_rels/pivotTable1.xml.rels.
|
|
590
|
+
rels = Relationships()
|
|
591
|
+
|
|
592
|
+
rels._add_document_relationship(
|
|
593
|
+
"/pivotCacheDefinition",
|
|
594
|
+
f"../pivotCache/pivotCacheDefinition{pivot_table.cache_id}.xml",
|
|
595
|
+
)
|
|
596
|
+
|
|
597
|
+
rels._set_xml_writer(
|
|
598
|
+
self._filename(f"xl/pivotTables/_rels/pivotTable{index}.xml.rels")
|
|
599
|
+
)
|
|
600
|
+
rels._assemble_xml_file()
|
|
601
|
+
|
|
602
|
+
def _write_pivot_cache_files(self) -> None:
|
|
603
|
+
# Write the pivot cache definition files.
|
|
604
|
+
for index, pivot_cache in enumerate(self.workbook.pivot_caches, 1):
|
|
605
|
+
pivot_cache._set_xml_writer(
|
|
606
|
+
self._filename(f"xl/pivotCache/pivotCacheDefinition{index}.xml")
|
|
607
|
+
)
|
|
608
|
+
pivot_cache._assemble_xml_file()
|
|
609
|
+
|
|
557
610
|
def _get_table_count(self) -> None:
|
|
558
611
|
# Count the table files. Required for the [Content_Types] file.
|
|
559
612
|
for worksheet in self.workbook.worksheets():
|
|
@@ -596,6 +649,11 @@ class Packager:
|
|
|
596
649
|
)
|
|
597
650
|
worksheet_index += 1
|
|
598
651
|
|
|
652
|
+
for i in range(1, self.pivot_cache_count + 1):
|
|
653
|
+
rels._add_document_relationship(
|
|
654
|
+
"/pivotCacheDefinition", f"pivotCache/pivotCacheDefinition{i}.xml"
|
|
655
|
+
)
|
|
656
|
+
|
|
599
657
|
rels._add_document_relationship("/theme", "theme/theme1.xml")
|
|
600
658
|
rels._add_document_relationship("/styles", "styles.xml")
|
|
601
659
|
|
|
@@ -638,6 +696,7 @@ class Packager:
|
|
|
638
696
|
+ worksheet.external_background_links
|
|
639
697
|
+ worksheet.external_table_links
|
|
640
698
|
+ worksheet.external_comment_links
|
|
699
|
+
+ worksheet.external_pivot_table_links
|
|
641
700
|
)
|
|
642
701
|
|
|
643
702
|
if not external_links:
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# PivotCache - A class for writing the Excel XLSX pivotCacheDefinition file.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: BSD-2-Clause
|
|
6
|
+
#
|
|
7
|
+
# Copyright (c) 2013-2026, John McNamara, jmcnamara@cpan.org
|
|
8
|
+
#
|
|
9
|
+
|
|
10
|
+
from typing import List, Tuple
|
|
11
|
+
|
|
12
|
+
from xlsxwriter import xmlwriter
|
|
13
|
+
from xlsxwriter.exceptions import PivotTableError
|
|
14
|
+
from xlsxwriter.utility import xl_range, xl_rowcol_to_cell
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class PivotCache(xmlwriter.XMLwriter):
|
|
18
|
+
"""
|
|
19
|
+
A class for writing the Excel XLSX pivotCacheDefinition file. The cache
|
|
20
|
+
doesn't contain any records and is refreshed by Excel when the file is
|
|
21
|
+
loaded.
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
###########################################################################
|
|
26
|
+
#
|
|
27
|
+
# Public API.
|
|
28
|
+
#
|
|
29
|
+
###########################################################################
|
|
30
|
+
|
|
31
|
+
def __init__(self, source: Tuple[str, int, int, int, int]) -> None:
|
|
32
|
+
"""
|
|
33
|
+
Constructor.
|
|
34
|
+
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
super().__init__()
|
|
38
|
+
|
|
39
|
+
self.source = source
|
|
40
|
+
self.field_names: List[str] = []
|
|
41
|
+
|
|
42
|
+
###########################################################################
|
|
43
|
+
#
|
|
44
|
+
# Private API.
|
|
45
|
+
#
|
|
46
|
+
###########################################################################
|
|
47
|
+
|
|
48
|
+
def _validate_field_names(self) -> None:
|
|
49
|
+
# Validate that the header cells in the source data are non-blank and
|
|
50
|
+
# unique, ignoring case, since Excel uses them as the pivot table field
|
|
51
|
+
# names.
|
|
52
|
+
sheet_name, first_row, first_col, _, _ = self.source
|
|
53
|
+
seen_names: List[str] = []
|
|
54
|
+
|
|
55
|
+
for col_offset, name in enumerate(self.field_names):
|
|
56
|
+
cell = xl_rowcol_to_cell(first_row, first_col + col_offset)
|
|
57
|
+
|
|
58
|
+
if not name:
|
|
59
|
+
raise PivotTableError(
|
|
60
|
+
f"Header cell '{sheet_name}!{cell}' in the pivot table data "
|
|
61
|
+
f"source is blank."
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
if name.lower() in seen_names:
|
|
65
|
+
raise PivotTableError(
|
|
66
|
+
f"Header '{name}' in cell '{sheet_name}!{cell}' is "
|
|
67
|
+
f"duplicated in the pivot table data source."
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
seen_names.append(name.lower())
|
|
71
|
+
|
|
72
|
+
def _assemble_xml_file(self) -> None:
|
|
73
|
+
# Assemble and write the XML file.
|
|
74
|
+
|
|
75
|
+
# Write the XML declaration.
|
|
76
|
+
self._xml_declaration()
|
|
77
|
+
|
|
78
|
+
# Write the pivotCacheDefinition element.
|
|
79
|
+
self._write_pivot_cache_definition()
|
|
80
|
+
|
|
81
|
+
# Write the cacheSource element.
|
|
82
|
+
self._write_cache_source()
|
|
83
|
+
|
|
84
|
+
# Write the cacheFields element.
|
|
85
|
+
self._write_cache_fields()
|
|
86
|
+
|
|
87
|
+
# Close the pivotCacheDefinition tag.
|
|
88
|
+
self._xml_end_tag("pivotCacheDefinition")
|
|
89
|
+
|
|
90
|
+
# Close the file.
|
|
91
|
+
self._xml_close()
|
|
92
|
+
|
|
93
|
+
###########################################################################
|
|
94
|
+
#
|
|
95
|
+
# XML methods.
|
|
96
|
+
#
|
|
97
|
+
###########################################################################
|
|
98
|
+
|
|
99
|
+
def _write_pivot_cache_definition(self) -> None:
|
|
100
|
+
# Write the <pivotCacheDefinition> element.
|
|
101
|
+
schema = "http://schemas.openxmlformats.org/"
|
|
102
|
+
xmlns = schema + "spreadsheetml/2006/main"
|
|
103
|
+
xmlns_r = schema + "officeDocument/2006/relationships"
|
|
104
|
+
_, first_row, _, last_row, _ = self.source
|
|
105
|
+
|
|
106
|
+
attributes = [
|
|
107
|
+
("xmlns", xmlns),
|
|
108
|
+
("xmlns:r", xmlns_r),
|
|
109
|
+
("saveData", 0),
|
|
110
|
+
("refreshOnLoad", 1),
|
|
111
|
+
("createdVersion", 3),
|
|
112
|
+
("refreshedVersion", 3),
|
|
113
|
+
("minRefreshableVersion", 3),
|
|
114
|
+
("recordCount", last_row - first_row),
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
self._xml_start_tag("pivotCacheDefinition", attributes)
|
|
118
|
+
|
|
119
|
+
def _write_cache_source(self) -> None:
|
|
120
|
+
# Write the <cacheSource> element.
|
|
121
|
+
attributes = [("type", "worksheet")]
|
|
122
|
+
|
|
123
|
+
self._xml_start_tag("cacheSource", attributes)
|
|
124
|
+
|
|
125
|
+
# Write the worksheetSource element.
|
|
126
|
+
self._write_worksheet_source()
|
|
127
|
+
|
|
128
|
+
self._xml_end_tag("cacheSource")
|
|
129
|
+
|
|
130
|
+
def _write_worksheet_source(self) -> None:
|
|
131
|
+
# Write the <worksheetSource> element.
|
|
132
|
+
sheet_name, first_row, first_col, last_row, last_col = self.source
|
|
133
|
+
|
|
134
|
+
attributes = [
|
|
135
|
+
("ref", xl_range(first_row, first_col, last_row, last_col)),
|
|
136
|
+
("sheet", sheet_name),
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
self._xml_empty_tag("worksheetSource", attributes)
|
|
140
|
+
|
|
141
|
+
def _write_cache_fields(self) -> None:
|
|
142
|
+
# Write the <cacheFields> element.
|
|
143
|
+
attributes = [("count", len(self.field_names))]
|
|
144
|
+
|
|
145
|
+
self._xml_start_tag("cacheFields", attributes)
|
|
146
|
+
|
|
147
|
+
for name in self.field_names:
|
|
148
|
+
# Write the cacheField element.
|
|
149
|
+
self._write_cache_field(name)
|
|
150
|
+
|
|
151
|
+
self._xml_end_tag("cacheFields")
|
|
152
|
+
|
|
153
|
+
def _write_cache_field(self, name: str) -> None:
|
|
154
|
+
# Write the <cacheField> element.
|
|
155
|
+
attributes = [
|
|
156
|
+
("name", name),
|
|
157
|
+
("numFmtId", 0),
|
|
158
|
+
]
|
|
159
|
+
|
|
160
|
+
self._xml_start_tag("cacheField", attributes)
|
|
161
|
+
|
|
162
|
+
# Write the sharedItems element.
|
|
163
|
+
self._xml_empty_tag("sharedItems")
|
|
164
|
+
|
|
165
|
+
self._xml_end_tag("cacheField")
|