Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
wb, from_xl, hwnd = None, None, None
for arg in sys.argv:
if arg.startswith('--wb='):
wb = arg.split('=')[1].strip()
elif arg.startswith('--from_xl'):
from_xl = arg.split('=')[1].strip()
elif arg.startswith('--hwnd'):
hwnd = arg.split('=')[1].strip()
if hasattr(Book, '_mock_caller'):
# Use mocking Book, see Book.set_mock_caller()
return cls(impl=Book._mock_caller.impl)
elif from_xl == '1':
fullname = wb.lower()
if sys.platform.startswith('win'):
app = App(impl=xlplatform.App(xl=int(hwnd)))
if not PY3 and isinstance(fullname, str):
fullname = fullname.decode('mbcs')
return cls(impl=app.books.open(fullname).impl)
else:
# On Mac, the same file open in two instances is not supported
if PY3 and apps.active.version < 15:
fullname = fullname.encode('utf-8', 'surrogateescape').decode('mac_latin2')
elif not PY3 and isinstance(fullname, str):
if apps.active.version < 15:
fullname = fullname.decode('mac_latin2')
else:
fullname = fullname.decode('utf-8')
return cls(impl=Book(fullname).impl)
elif xlplatform.BOOK_CALLER:
# Called via OPTIMIZED_CONNECTION = True
return cls(impl=xlplatform.Book(xlplatform.BOOK_CALLER))
def __eq__(self, other):
return type(other) is App and other.pid == self.pid
add_to_mru=None, local=None, corrupt_load=None, impl=None):
if not impl:
if fullname:
fullname = utils.fspath(fullname)
fullname = fullname.lower()
candidates = []
for app in apps:
for wb in app.books:
if wb.fullname.lower() == fullname or wb.name.lower() == fullname:
candidates.append((app, wb))
app = apps.active
if len(candidates) == 0:
if not app:
app = App(add_book=False)
impl = app.books.open(fullname, update_links, read_only, format, password, write_res_password,
ignore_read_only_recommended, origin, delimiter, editable, notify, converter,
add_to_mru, local, corrupt_load).impl
elif len(candidates) > 1:
raise Exception("Workbook '%s' is open in more than one Excel instance." % fullname)
else:
impl = candidates[0][1].impl
else:
# Open Excel if necessary and create a new workbook
if apps.active:
impl = apps.active.books.add().impl
else:
app = App()
impl = app.books[0].impl
self.impl = impl
if len(candidates) == 0:
if not app:
app = App(add_book=False)
impl = app.books.open(fullname, update_links, read_only, format, password, write_res_password,
ignore_read_only_recommended, origin, delimiter, editable, notify, converter,
add_to_mru, local, corrupt_load).impl
elif len(candidates) > 1:
raise Exception("Workbook '%s' is open in more than one Excel instance." % fullname)
else:
impl = candidates[0][1].impl
else:
# Open Excel if necessary and create a new workbook
if apps.active:
impl = apps.active.books.add().impl
else:
app = App()
impl = app.books[0].impl
self.impl = impl
def app(self):
"""
Returns an app object that represents the creator of the book.
.. versionadded:: 0.9.0
"""
return App(impl=self.impl.app)
def add(self):
"""
Creates a new App. The new App becomes the active one. Returns an App object.
"""
return App()
def __iter__(self):
for app in self.impl:
yield App(impl=app)
def __getitem__(self, item):
return App(impl=self.impl[item])
def active(self):
"""
Returns the active app.
.. versionadded:: 0.9.0
"""
for app in self.impl:
return App(impl=app)
return None