Skip to content

Commit 3c9a9d2

Browse files
committed
Add 4 new functions: delete_defined_name, get_cell_style, remove_page_break and remove_row
- Update unit tests and docs for the function
1 parent 0e11565 commit 3c9a9d2

File tree

8 files changed

+196
-15
lines changed

8 files changed

+196
-15
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2024 The excelize Authors.
3+
Copyright (c) 2024 - 2025 The excelize Authors.
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or without

excelize.py

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
2-
code is governed by a BSD-style license that can be found in the LICENSE file.
1+
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
2+
source code is governed by a BSD-style license that can be found in the LICENSE
3+
file.
34
45
Package excelize-py is a Python port of Go Excelize library, providing a set of
56
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX
@@ -1079,6 +1080,33 @@ def delete_comment(self, sheet: str, cell: str) -> Optional[Exception]:
10791080
).decode(ENCODE)
10801081
return None if err == "" else Exception(err)
10811082

1083+
def delete_defined_name(self, defined_name: DefinedName) -> Optional[Exception]:
1084+
"""
1085+
Delete the defined names of the workbook or worksheet. If not specified
1086+
scope, the default scope is workbook.
1087+
1088+
Args:
1089+
defined_name (DefinedName): The defined name options
1090+
1091+
Returns:
1092+
Optional[Exception]: Returns None if no error occurred,
1093+
otherwise returns an Exception with the message.
1094+
1095+
Example:
1096+
For example:
1097+
1098+
.. code-block:: python
1099+
1100+
err = f.delete_defined_name(excelize.DefinedName(
1101+
name="Amount",
1102+
scope="Sheet2",
1103+
))
1104+
"""
1105+
lib.DeleteDefinedName.restype = c_char_p
1106+
options = py_value_to_c(defined_name, types_go._DefinedName())
1107+
err = lib.DeleteDefinedName(self.file_index, byref(options)).decode(ENCODE)
1108+
return None if err == "" else Exception(err)
1109+
10821110
def delete_picture(self, sheet: str, cell: str) -> Optional[Exception]:
10831111
"""
10841112
Delete all pictures in a cell by given worksheet name and cell reference.
@@ -1265,6 +1293,25 @@ def get_cell_hyperlink(
12651293
None if err == "" else Exception(err),
12661294
)
12671295

1296+
def get_cell_style(self, sheet: str, cell: str) -> Tuple[int, Optional[Exception]]:
1297+
"""
1298+
Get cell style index by given worksheet name and cell reference.
1299+
1300+
Args:
1301+
sheet (str): The worksheet name
1302+
cell (str): The cell reference
1303+
1304+
Returns:
1305+
Tuple[int, Optional[Exception]]: A tuple containing the cell style,
1306+
and an Exception object if an error occurred, otherwise None.
1307+
"""
1308+
lib.GetCellStyle.restype = types_go._IntErrorResult
1309+
res = lib.GetCellStyle(
1310+
self.file_index, sheet.encode(ENCODE), cell.encode(ENCODE)
1311+
)
1312+
err = res.err.decode(ENCODE)
1313+
return res.val, None if err == "" else Exception(err)
1314+
12681315
def get_cell_value(
12691316
self, sheet: str, cell: str, *opts: Options
12701317
) -> Tuple[str, Optional[Exception]]:
@@ -1650,6 +1697,53 @@ def remove_col(self, sheet: str, col: str) -> Optional[Exception]:
16501697
).decode(ENCODE)
16511698
return None if err == "" else Exception(err)
16521699

1700+
def remove_page_break(self, sheet: str, cell: str) -> Optional[Exception]:
1701+
"""
1702+
Remove a page break by given worksheet name and cell reference.
1703+
1704+
Args:
1705+
sheet (str): The worksheet name
1706+
cell (str): The cell reference
1707+
1708+
Returns:
1709+
Optional[Exception]: Returns None if no error occurred,
1710+
otherwise returns an Exception with the message.
1711+
"""
1712+
lib.RemovePageBreak.restype = c_char_p
1713+
err = lib.RemovePageBreak(
1714+
self.file_index, sheet.encode(ENCODE), cell.encode(ENCODE)
1715+
).decode(ENCODE)
1716+
return None if err == "" else Exception(err)
1717+
1718+
def remove_row(self, sheet: str, row: int) -> Optional[Exception]:
1719+
"""
1720+
Remove single row by given worksheet name and Excel row number. Use this
1721+
method with caution, which will affect changes in references such as
1722+
formulas, charts, and so on. If there is any referenced value of the
1723+
worksheet, it will cause a file error when you open it. The excelize
1724+
only partially updates these references currently.
1725+
1726+
Args:
1727+
sheet (str): The worksheet name
1728+
row (int): The row number
1729+
1730+
Returns:
1731+
Optional[Exception]: Returns None if no error occurred,
1732+
otherwise returns an Exception with the message.
1733+
1734+
Example:
1735+
For example, remove row 3 in Sheet1:
1736+
1737+
.. code-block:: python
1738+
1739+
err = f.remove_row("Sheet1", 3)
1740+
"""
1741+
lib.RemoveRow.restype = c_char_p
1742+
err = lib.RemoveRow(self.file_index, sheet.encode(ENCODE), c_int(row)).decode(
1743+
ENCODE
1744+
)
1745+
return None if err == "" else Exception(err)
1746+
16531747
def set_active_sheet(self, index: int) -> Optional[Exception]:
16541748
"""
16551749
Set the default active sheet of the workbook by a given index. Note that

main.go

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// Copyright 2024 The excelize Authors. All rights reserved. Use of this source
2-
// code is governed by a BSD-style license that can be found in the LICENSE file.
1+
// Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
2+
// source code is governed by a BSD-style license that can be found in the
3+
// LICENSE file.
34
//
45
// Package excelize-py is a Python port of Go Excelize library, providing a set
56
// of functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM
@@ -881,6 +882,28 @@ func DeleteComment(idx int, sheet, cell *C.char) *C.char {
881882
return C.CString(emptyString)
882883
}
883884

885+
// DeleteDefinedName provides a function to delete the defined names of the
886+
// workbook or worksheet. If not specified scope, the default scope is
887+
// workbook.
888+
//
889+
//export DeleteDefinedName
890+
func DeleteDefinedName(idx int, definedName *C.struct_DefinedName) *C.char {
891+
var df excelize.DefinedName
892+
goVal, err := cValueToGo(reflect.ValueOf(*definedName), reflect.TypeOf(excelize.DefinedName{}))
893+
if err != nil {
894+
return C.CString(err.Error())
895+
}
896+
df = goVal.Elem().Interface().(excelize.DefinedName)
897+
f, ok := files.Load(idx)
898+
if !ok {
899+
return C.CString(errFilePtr)
900+
}
901+
if err := f.(*excelize.File).DeleteDefinedName(&df); err != nil {
902+
return C.CString(err.Error())
903+
}
904+
return C.CString(emptyString)
905+
}
906+
884907
// DeletePicture provides a function to delete charts in spreadsheet by given
885908
// worksheet name and cell reference. Note that the image file won't be
886909
// deleted from the document currently.
@@ -1031,6 +1054,22 @@ func GetCellHyperLink(idx int, sheet, cell *C.char) C.struct_GetCellHyperLinkRes
10311054
return C.struct_GetCellHyperLinkResult{link: C._Bool(link), target: C.CString(target), err: C.CString(emptyString)}
10321055
}
10331056

1057+
// GetCellStyle provides a function to get cell style index by given worksheet
1058+
// name and cell reference. This function is concurrency safe.
1059+
//
1060+
//export GetCellStyle
1061+
func GetCellStyle(idx int, sheet, cell *C.char) C.struct_IntErrorResult {
1062+
f, ok := files.Load(idx)
1063+
if !ok {
1064+
return C.struct_IntErrorResult{val: C.int(0), err: C.CString(errFilePtr)}
1065+
}
1066+
idx, err := f.(*excelize.File).GetCellStyle(C.GoString(sheet), C.GoString(cell))
1067+
if err != nil {
1068+
return C.struct_IntErrorResult{val: C.int(idx), err: C.CString(err.Error())}
1069+
}
1070+
return C.struct_IntErrorResult{val: C.int(idx), err: C.CString(emptyString)}
1071+
}
1072+
10341073
// GetCellValue provides a function to get formatted value from cell by given
10351074
// worksheet name and cell reference in spreadsheet. The return value is
10361075
// converted to the `string` data type. If the cell format can be applied to
@@ -1430,6 +1469,36 @@ func RemoveCol(idx int, sheet, col *C.char) *C.char {
14301469
return C.CString(emptyString)
14311470
}
14321471

1472+
// RemovePageBreak remove a page break by given worksheet name and cell
1473+
// reference.
1474+
//
1475+
//export RemovePageBreak
1476+
func RemovePageBreak(idx int, sheet, cell *C.char) *C.char {
1477+
f, ok := files.Load(idx)
1478+
if !ok {
1479+
return C.CString(errFilePtr)
1480+
}
1481+
if err := f.(*excelize.File).RemovePageBreak(C.GoString(sheet), C.GoString(cell)); err != nil {
1482+
return C.CString(err.Error())
1483+
}
1484+
return C.CString(emptyString)
1485+
}
1486+
1487+
// RemoveRow provides a function to remove single row by given worksheet name
1488+
// and Excel row number.
1489+
//
1490+
//export RemoveRow
1491+
func RemoveRow(idx int, sheet *C.char, row int) *C.char {
1492+
f, ok := files.Load(idx)
1493+
if !ok {
1494+
return C.CString(errFilePtr)
1495+
}
1496+
if err := f.(*excelize.File).RemoveRow(C.GoString(sheet), row); err != nil {
1497+
return C.CString(err.Error())
1498+
}
1499+
return C.CString(emptyString)
1500+
}
1501+
14331502
// Save provides a function to override the spreadsheet with origin path.
14341503
//
14351504
//export Save

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
2-
code is governed by a BSD-style license that can be found in the LICENSE file.
1+
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
2+
source code is governed by a BSD-style license that can be found in the LICENSE
3+
file.
34
45
Package excelize-py is a Python port of Go Excelize library, providing a set of
56
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX

test_excelize.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
2-
code is governed by a BSD-style license that can be found in the LICENSE file.
1+
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
2+
source code is governed by a BSD-style license that can be found in the LICENSE
3+
file.
34
45
Package excelize-py is a Python port of Go Excelize library, providing a set of
56
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX
@@ -118,6 +119,9 @@ def test_style(self):
118119
self.assertIsNone(err)
119120
self.assertEqual(style, s)
120121
self.assertIsNone(f.set_cell_style("Sheet1", "A1", "B2", style_id))
122+
result, err = f.get_cell_style("Sheet1", "A2")
123+
self.assertIsNone(err)
124+
self.assertEqual(result, style_id)
121125
self.assertEqual(
122126
str(f.set_cell_style("SheetN", "A1", "B2", style_id)),
123127
"sheet SheetN does not exist",
@@ -289,6 +293,8 @@ def test_style(self):
289293
)
290294
self.assertIsNone(f.move_sheet("Sheet2", "Sheet1"))
291295
self.assertIsNone(f.remove_col("Sheet1", "Z"))
296+
self.assertIsNone(f.remove_page_break("Sheet1", "A1"))
297+
self.assertIsNone(f.remove_row("Sheet1", 100))
292298
self.assertIsNone(f.ungroup_sheets())
293299
self.assertIsNone(f.update_linked_value())
294300
self.assertIsNone(f.save())
@@ -1032,6 +1038,14 @@ def test_defined_name(self):
10321038
)
10331039
)
10341040
)
1041+
self.assertIsNone(
1042+
f.delete_defined_name(
1043+
excelize.DefinedName(
1044+
name="Amount",
1045+
scope="Sheet1",
1046+
)
1047+
)
1048+
)
10351049
self.assertIsNone(f.save_as(os.path.join("test", "TestSetDefinedName.xlsx")))
10361050
self.assertIsNone(f.close())
10371051

types_c.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// Copyright 2024 The excelize Authors. All rights reserved. Use of this source
2-
// code is governed by a BSD-style license that can be found in the LICENSE file.
1+
// Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
2+
// source code is governed by a BSD-style license that can be found in the
3+
// LICENSE file.
34
//
45
// Package excelize-py is a Python port of Go Excelize library, providing a set
56
// of functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM

types_go.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
2-
code is governed by a BSD-style license that can be found in the LICENSE file.
1+
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
2+
source code is governed by a BSD-style license that can be found in the LICENSE
3+
file.
34
45
Package excelize-py is a Python port of Go Excelize library, providing a set of
56
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX

types_py.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
2-
code is governed by a BSD-style license that can be found in the LICENSE file.
1+
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
2+
source code is governed by a BSD-style license that can be found in the LICENSE
3+
file.
34
45
Package excelize-py is a Python port of Go Excelize library, providing a set of
56
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX

0 commit comments

Comments
 (0)