|
25 | 25 | if xw is not None:
|
26 | 26 | from xlwings.conversion.pandas_conv import PandasDataFrameConverter
|
27 | 27 |
|
| 28 | + from xlwings.constants import FileFormat |
28 | 29 | global_app = None
|
29 | 30 |
|
30 | 31 |
|
@@ -257,12 +258,32 @@ def __delitem__(self, key):
|
257 | 258 | def sheet_names(self):
|
258 | 259 | return [s.name for s in self]
|
259 | 260 |
|
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 | + """ |
261 | 271 | # saved_path = self.xw_wkb.api.Path
|
262 | 272 | # was_saved = saved_path != ''
|
263 | 273 | if path is None and self.delayed_filepath is not None:
|
264 | 274 | 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) |
266 | 287 |
|
267 | 288 | def close(self):
|
268 | 289 | # Close the workbook in Excel.
|
|
0 commit comments