Skip to content

Commit 6fb33e0

Browse files
authored
Add new function: unprotect_workbook (#26)
1 parent 7264f79 commit 6fb33e0

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

excelize.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5096,6 +5096,27 @@ def unprotect_sheet(self, sheet: str, *password: str) -> None:
50965096
if err != "":
50975097
raise RuntimeError(err)
50985098

5099+
def unprotect_workbook(self, *password: str) -> None:
5100+
"""
5101+
Remove protection for workbook, specified the optional password
5102+
parameter to remove workbook protection with password verification.
5103+
5104+
Args:
5105+
*password (str): Optional password for workbook protection
5106+
verification
5107+
5108+
Returns:
5109+
None: Return None if no error occurred, otherwise raise a
5110+
RuntimeError with the message.
5111+
"""
5112+
lib.UnprotectWorkbook.restype = c_char_p
5113+
passwd = password[0] if len(password) > 0 else ""
5114+
err = lib.UnprotectWorkbook(
5115+
self.file_index, passwd.encode(ENCODE), len(password) > 0
5116+
).decode(ENCODE)
5117+
if err != "":
5118+
raise RuntimeError(err)
5119+
50995120
def update_linked_value(self) -> None:
51005121
"""
51015122
Fix linked values within a spreadsheet are not updating in Office Excel

main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2785,6 +2785,28 @@ func UnprotectSheet(idx int, sheet, password *C.char, verify bool) *C.char {
27852785
return C.CString(emptyString)
27862786
}
27872787

2788+
// UnprotectWorkbook provides a function to remove protection for workbook,
2789+
// specified the optional password parameter to remove workbook protection with
2790+
// password verification.
2791+
//
2792+
//export UnprotectWorkbook
2793+
func UnprotectWorkbook(idx int, password *C.char, verify bool) *C.char {
2794+
f, ok := files.Load(idx)
2795+
if !ok {
2796+
return C.CString(errFilePtr)
2797+
}
2798+
if verify {
2799+
if err := f.(*excelize.File).UnprotectWorkbook(C.GoString(password)); err != nil {
2800+
return C.CString(err.Error())
2801+
}
2802+
return C.CString(emptyString)
2803+
}
2804+
if err := f.(*excelize.File).UnprotectWorkbook(); err != nil {
2805+
return C.CString(err.Error())
2806+
}
2807+
return C.CString(emptyString)
2808+
}
2809+
27882810
// UpdateLinkedValue fix linked values within a spreadsheet are not updating in
27892811
// Office Excel application. This function will be remove value tag when met a
27902812
// cell have a linked value.

test_excelize.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@ def test_style(self):
468468
)
469469
)
470470
)
471+
with self.assertRaises(RuntimeError) as context:
472+
f.unprotect_workbook("")
473+
self.assertEqual(str(context.exception), "password length invalid")
474+
self.assertIsNone(f.unprotect_workbook("password"))
475+
self.assertIsNone(f.unprotect_workbook())
471476
self.assertIsNone(f.move_sheet("Sheet2", "Sheet1"))
472477
with self.assertRaises(RuntimeError) as context:
473478
f.move_sheet("SheetN", "Sheet1")
@@ -1569,15 +1574,15 @@ def test_sheet_visible(self):
15691574
f.set_sheet_visible("Sheet:1", False, True)
15701575
self.assertEqual(
15711576
str(context.exception),
1572-
"the sheet can not contain any of the characters :\/?*[or]",
1577+
"the sheet can not contain any of the characters :\\/?*[or]",
15731578
)
15741579
self.assertTrue(f.get_sheet_visible("Sheet1"))
15751580
self.assertFalse(f.get_sheet_visible("Sheet2"))
15761581
with self.assertRaises(RuntimeError) as context:
15771582
f.get_sheet_visible("Sheet:1")
15781583
self.assertEqual(
15791584
str(context.exception),
1580-
"the sheet can not contain any of the characters :\/?*[or]",
1585+
"the sheet can not contain any of the characters :\\/?*[or]",
15811586
)
15821587
self.assertIsNone(f.save_as(os.path.join("test", "TestSheetVisible.xlsx")))
15831588
self.assertIsNone(f.close())

0 commit comments

Comments
 (0)