在Python中对行和列进行分组和取消分组
Contents
[
Hide
]
在Aspose.Cells for Python via Java中对行和列进行分组和取消分组管理
如何在Python中对行和列进行分组
可以通过调用Cells集合的groupRows和groupColumns方法来对行或列进行分组。这两个方法都接受以下参数:
- 第一个行/列索引,即组中的第一行或列。
- 最后一个行/列索引,即组中的最后一行或列。
- 是否隐藏,一个布尔参数,指定是否在分组后隐藏行/列。
Python 代码
def group_rows_columns(self):
\# Instantiating a Workbook object by excel file path
workbook = self.Workbook(self.dataDir + 'Book1.xls')
\# Accessing the first worksheet in the Excel file
worksheet = workbook.getWorksheets().get(0)
cells = worksheet.getCells()
\# Grouping first six rows (from 0 to 5) and making them hidden by passing true
cells.groupRows(0,5,True)
\# Grouping first three columns (from 0 to 2) and making them hidden by passing true
cells.groupColumns(0,2,True)
\# Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(self.dataDir + "Group Rows And Columns.xls")
print "Group Rows And Columns Successfully."
如何使用Python取消对行和列进行分组
通过调用Cells集合的UngroupRows和UngroupColumns方法来取消分组的行或列。这两个方法接受相同的参数:
- 第一个行或列索引,即要取消分组的第一行/列。
- 最后一个行或列索引,即要取消分组的最后一行/列。
Python 代码
def ungroup_rows_columns(self):
\# Instantiating a Workbook object by excel file path
workbook = self.Workbook(self.dataDir + 'Group Rows And Columns.xls')
\# Accessing the first worksheet in the Excel file
worksheet = workbook.getWorksheets().get(0)
cells = worksheet.getCells()
\# Ungrouping first six rows (from 0 to 5)
cells.ungroupRows(0,5)
\# Ungrouping first three columns (from 0 to 2)
cells.ungroupColumns(0,2)
\# Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(self.dataDir + "Ungroup Rows And Columns.xls")
print "Ungroup Rows And Columns Successfully."
下载运行代码
从以下任何社交编码网站下载分组和取消分组行和列(Aspose.Cells):