Dar formato a la tabla dinámica
Apariencia de la tabla dinámica
Cómo crear una tabla dinámica explica cómo crear una tabla dinámica simple. Este artículo describe cómo personalizar la apariencia de una tabla dinámica estableciendo diversas propiedades:
- Opciones de formato de tabla dinámica
- Opciones de formato de campos de tabla dinámica
- Opciones de formato de campos de datos
Cómo establecer opciones de formato de tabla dinámica
La clase PivotTable controla la tabla dinámica general y se puede formatear de varias maneras.
Cómo establecer el tipo de autoformato
Microsoft Excel ofrece una serie de formatos de informe preestablecidos diferentes. Aspose.Cells for Python via .NET también admite estas opciones de formato. Para acceder a ellos:
- Establezca PivotTable.is_auto_format en verdadero.
- Asignar una opción de formato de la enumeración PivotTableAutoFormatType.
from aspose.cells import Workbook | |
from aspose.cells.pivot import PivotTableAutoFormatType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Load a template file | |
workbook = Workbook(dataDir + "Book1.xls") | |
pivotindex = 0 | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Accessing the PivotTable | |
pivotTable = worksheet.pivot_tables[pivotindex] | |
# Setting the PivotTable report is automatically formatted | |
pivotTable.is_auto_format = True | |
# Setting the PivotTable atuoformat type. | |
pivotTable.auto_format_type = PivotTableAutoFormatType.REPORT5 | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
Cómo establecer opciones de formato
El ejemplo de código a continuación muestra cómo dar formato a la tabla dinámica para mostrar totales generales para filas y columnas, y cómo establecer el orden de los campos del informe. También muestra cómo establecer una cadena personalizada para los valores nulos.
from aspose.cells import PrintOrderType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Load a template file | |
workbook = Workbook(dataDir + "Book1.xls") | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
pivotindex = 0 | |
# Accessing the PivotTable | |
pivotTable = worksheet.pivot_tables[pivotindex] | |
# Setting the PivotTable report shows grand totals for rows. | |
pivotTable.row_grand = True | |
# Setting the PivotTable report shows grand totals for columns. | |
pivotTable.column_grand = True | |
# Setting the PivotTable report displays a custom string in cells that contain null values. | |
pivotTable.display_null_string = True | |
pivotTable.null_string = "null" | |
# Setting the PivotTable report's layout | |
pivotTable.page_field_order = PrintOrderType.DOWN_THEN_OVER | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
Dar formato manualmente al aspecto visual
Para formatear cómo se ve el informe de tabla dinámica manualmente, en lugar de usar formatos de informe preestablecidos, use los métodos PivotTable.format_all(style) y PivotTable.format(row, column, style). Cree un objeto de estilo para su formato deseado, por ejemplo:
from aspose.cells import BackgroundType, Workbook | |
from aspose.cells.pivot import PivotTableStyleType | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Load a template file | |
workbook = Workbook(dataDir + "Book1.xls") | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
pivot = workbook.worksheets[0].pivot_tables[0] | |
pivot.pivot_table_style_type = PivotTableStyleType.PIVOT_TABLE_STYLE_DARK1 | |
style = workbook.create_style() | |
style.font.name = "Arial Black" | |
style.foreground_color = Color.yellow | |
style.pattern = BackgroundType.SOLID | |
pivot.format_all(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
Cómo establecer opciones de formato de campo de tabla dinámica
La clase PivotField representa un campo en una tabla dinámica y se puede formatear de varias formas. El ejemplo de código a continuación muestra cómo:
- Acceder a los campos de fila.
- Establecer subtotales.
- Establecer orden automático.
- Establecer autover.
Cómo establecer el formato de campos de fila/columna/página.
from aspose.cells import Workbook | |
from aspose.cells.pivot import PivotFieldSubtotalType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Load a template file | |
workbook = Workbook(dataDir + "Book1.xls") | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
pivotindex = 0 | |
# Accessing the PivotTable | |
pivotTable = worksheet.pivot_tables[pivotindex] | |
# Setting the PivotTable report shows grand totals for rows. | |
pivotTable.row_grand = True | |
# Accessing the row fields. | |
pivotFields = pivotTable.row_fields | |
# Accessing the first row field in the row fields. | |
pivotField = pivotFields[0] | |
# Setting Subtotals. | |
pivotField.set_subtotals(PivotFieldSubtotalType.SUM, True) | |
pivotField.set_subtotals(PivotFieldSubtotalType.COUNT, True) | |
# Setting autosort options. | |
# Setting the field auto sort. | |
pivotField.is_auto_sort = True | |
# Setting the field auto sort ascend. | |
pivotField.is_ascend_sort = True | |
# Setting the field auto sort using the field itself. | |
pivotField.auto_sort_field = -5 | |
# Setting autoShow options. | |
# Setting the field auto show. | |
pivotField.is_auto_show = True | |
# Setting the field auto show ascend. | |
pivotField.is_ascend_show = False | |
# Setting the auto show using field(data field). | |
pivotField.auto_show_field = 0 | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
Cómo establecer el formato de campos de datos.
El ejemplo de código a continuación muestra cómo establecer formatos de visualización y formato numérico para los campos de datos.
from aspose.cells import Workbook | |
from aspose.cells.pivot import PivotFieldDataDisplayFormat, PivotItemPosition | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Load a template file | |
workbook = Workbook(dataDir + "Book1.xls") | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
pivotindex = 0 | |
# Accessing the PivotTable | |
pivotTable = worksheet.pivot_tables[pivotindex] | |
# Accessing the data fields. | |
pivotFields = pivotTable.data_fields | |
# Accessing the first data field in the data fields. | |
pivotField = pivotFields[0] | |
# Setting data display format | |
pivotField.data_display_format = PivotFieldDataDisplayFormat.PERCENTAGE_OF | |
# Setting the base field. | |
pivotField.base_field_index = 1 | |
# Setting the base item. | |
pivotField.base_item_position = PivotItemPosition.NEXT | |
# Setting number format | |
pivotField.number = 10 | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
Cómo limpiar los campos de filtro.
La clase PivotFieldCollection tiene un método llamado clear() que te permite borrar campos de tabla dinámica. Úsalo cuando quieras borrar todos los campos de tabla dinámica en las áreas, por ejemplo, página, columna, fila o datos. El ejemplo de código a continuación muestra cómo borrar todos los campos de tabla dinámica en un área de datos.
from aspose.cells import Workbook | |
from aspose.cells.pivot import PivotFieldType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Load a template file | |
workbook = Workbook(dataDir + "Book1.xls") | |
# Get the first worksheet | |
sheet = workbook.worksheets[0] | |
# Get the pivot tables in the sheet | |
pivotTables = sheet.pivot_tables | |
# Get the first PivotTable | |
pivotTable = pivotTables[0] | |
# Clear all the data fields | |
pivotTable.data_fields.clear() | |
# Add new data field | |
pivotTable.add_field_to_area(PivotFieldType.DATA, "Betrag Netto FW") | |
# Set the refresh data flag on | |
pivotTable.refresh_data_flag = False | |
# Refresh and calculate the pivot table data | |
pivotTable.refresh_data() | |
pivotTable.calculate_data() | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |