格式化数据透视表单元格
Contents
[
Hide
]
有时候,您想要格式化数据透视表单元格。例如,您想要将背景颜色应用到数据透视表单元格。Aspose.Cells for Python via .NET 提供了两种方法 PivotTable.format_all(style) 和 PivotTable.format(row, column, style),您可以用于此目的。
PivotTable.format_all(style) 将样式应用到整个数据透视表,而 PivotTable.format(row, column, style) 将样式应用到数据透视表的单个单元格。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import BackgroundType, Workbook | |
from aspose.pydrawing import Color | |
# Create workbook object from source file containing pivot table | |
workbook = Workbook("pivot_format.xlsx") | |
# Access the worksheet by its name | |
worksheet = workbook.worksheets.get("Sheet1") | |
# Access the pivot table | |
pivotTable = worksheet.pivot_tables[1] | |
# Create a style object with background color light blue | |
style = workbook.create_style() | |
style.pattern = BackgroundType.SOLID | |
style.background_color = Color.light_blue | |
# Format entire pivot table with light blue color | |
pivotTable.format_all(style) | |
# Create another style object with yellow color | |
style = workbook.create_style() | |
style.pattern = BackgroundType.SOLID | |
style.background_color = Color.yellow | |
# Access the pivot table | |
pivotTable2 = worksheet.pivot_tables[0] | |
# Format the cell of pivot table | |
pivotTable2.format(16, 5, style) | |
# Save the workbook object | |
workbook.save("out.xlsx") |