Skip to content

Commit 8568cf8

Browse files
committed
added support for saving an Excel file with a password
1 parent 4910499 commit 8568cf8

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

doc/source/changes/version_0_30.rst.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ Miscellaneous improvements
359359
- light : to output axes labels only when they change instead of repeating them on each line
360360
- na_repr : to specify how to represent N/A (NaN) values
361361

362+
* added password argument to :py:obj:`Workbook.save()` to allow protecting Excel files with a password.
363+
362364
* substantially improved performance of creating, iterating, and doing a few other operations over larray objects.
363365
This solves a few pathological cases of slow operations, especially those involving many small-ish arrays but sadly
364366
the overall performance improvement is negligible over most of the real-world models using larray that we tested these

larray/inout/xw_excel.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
if xw is not None:
2626
from xlwings.conversion.pandas_conv import PandasDataFrameConverter
2727

28+
from xlwings.constants import FileFormat
2829
global_app = None
2930

3031

@@ -257,12 +258,32 @@ def __delitem__(self, key):
257258
def sheet_names(self):
258259
return [s.name for s in self]
259260

260-
def save(self, path=None):
261+
def save(self, path=None, password=None):
262+
r"""Save Workbook to file.
263+
264+
Parameters
265+
----------
266+
path : str, optional
267+
Path to save the file to. Defaults to None (use the path used when opening the workbook).
268+
password : str, optional
269+
Password to protect the file. Defaults to None (no password).
270+
"""
261271
# saved_path = self.xw_wkb.api.Path
262272
# was_saved = saved_path != ''
263273
if path is None and self.delayed_filepath is not None:
264274
path = self.delayed_filepath
265-
self.xw_wkb.save(path=path)
275+
276+
if password is not None:
277+
if path is None:
278+
raise ValueError("saving a Workbook with a password is only supported for workbooks with an "
279+
"explicit path (given either when opening the workbook or here as the path "
280+
"argument)")
281+
realpath = os.path.realpath(path)
282+
# XXX: this is probably Windows only
283+
# using Password as keyword argument does not work !
284+
self.xw_wkb.api.SaveAs(realpath, FileFormat.xlOpenXMLWorkbook, password)
285+
else:
286+
self.xw_wkb.save(path=path)
266287

267288
def close(self):
268289
# Close the workbook in Excel.

0 commit comments

Comments
 (0)