Skip to content

Commit

Permalink
added Workbook.save method, closes xlwings#110
Browse files Browse the repository at this point in the history
  • Loading branch information
fzumstein committed Jan 16, 2015
1 parent 69fef53 commit 7bfe1bc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
15 changes: 13 additions & 2 deletions xlwings/_xlmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
import datetime as dt
from appscript import app
from appscript import app, mactypes
from appscript import k as kw
import psutil
import atexit
Expand Down Expand Up @@ -363,4 +363,15 @@ def get_xl_workbook_from_xl(fullname):
Doesn't really do anything on Mac, but on Windows, this is needed due to some
Workbooks not turning up in the RunningObjectTable
"""
return get_workbook(fullname)[1]
return get_workbook(fullname)[1]


def save_workbook(xl_workbook, path):
if path is None:
path = os.path.join(os.getcwd(), xl_workbook.name.get())

dir_name, file_name = os.path.split(path)
dir_name_hfs = mactypes.Alias(dir_name).hfspath # turn into HFS path format
hfs_path = dir_name_hfs + ':' + file_name

xl_workbook.save_workbook_as(filename=hfs_path)
9 changes: 8 additions & 1 deletion xlwings/_xlwindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,11 @@ def get_xl_workbook_from_xl(fullname):
"and is opened in the first instance of Excel.")
else:
xl_workbook = GetObject(fullname)
return xl_workbook
return xl_workbook


def save_workbook(xl_workbook, path):
if path is None:
path = os.path.join(os.getcwd(), xl_workbook.Name)

xl_workbook.SaveAs(path)
20 changes: 19 additions & 1 deletion xlwings/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, fullname=None, xl_workbook=None):
self.xl_workbook = xl_workbook
self.xl_app = xlplatform.get_app(self.xl_workbook)
elif fullname:
self.fullname = fullname.lower()
self.fullname = fullname
if xlplatform.is_file_open(self.fullname):
# Connect to an open Workbook
self.xl_app, self.xl_workbook = xlplatform.get_workbook(self.fullname)
Expand Down Expand Up @@ -158,6 +158,24 @@ def close(self):
"""Closes the Workbook without saving it."""
xlplatform.close_workbook(self.xl_workbook)

def save(self, path=None):
"""
Saves the Workbook under the specified path.
Arguments
---------
path : str
Full path to the workbook, e.g. '/path/to/file_name.xlsx'
Example
-------
>>> from xlwings import Workbook
>>> wb = Workbook()
>>> wb.save(r'C:\\path\\to\\file.xlsx')
"""
xlplatform.save_workbook(self.xl_workbook, path)

@staticmethod
def get_xl_workbook(wkb):
"""
Expand Down
35 changes: 35 additions & 0 deletions xlwings/tests/test_xlwings.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,41 @@ def test_reference_two_unsaved_wb(self):
wb1.close()
wb2.close()

def test_save_naked(self):

cwd = os.getcwd()
wb1 = Workbook()
target_file_path = os.path.join(cwd, wb1.name)
if os.path.isfile(target_file_path):
os.remove(target_file_path)

wb1.save()

assert_equal(os.path.isfile(target_file_path), True)

wb1.close()

if os.path.isfile(target_file_path):
os.remove(target_file_path)

def test_save_path(self):

cwd = os.getcwd()
wb1 = Workbook()
target_file_path = os.path.join(cwd, 'TestFile.xlsx')
if os.path.isfile(target_file_path):
os.remove(target_file_path)

wb1.save(target_file_path)

assert_equal(os.path.isfile(target_file_path), True)

wb2 = Workbook(target_file_path)
wb2.close()

if os.path.isfile(target_file_path):
os.remove(target_file_path)


class TestSheet:
def setUp(self):
Expand Down

0 comments on commit 7bfe1bc

Please sign in to comment.