Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sheet : Sheet, default None
Sheet object. If none provided, the first sheet of a new workbook is used.
Examples
--------
>>> import xlwings as xw
>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
>>> xw.view(df)
.. versionadded:: 0.7.1
"""
if sheet is None:
sheet = Book().sheets.active
else:
sheet.clear()
sheet.range('A1').value = obj
sheet.autofit()
wb = xw.Book.caller()
wb.sheets[0].range('A1').value = 1
To be able to easily invoke such code from Python for debugging, use ``xw.Book.set_mock_caller()``.
.. versionadded:: 0.3.0
"""
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')
def add(self):
"""
Creates a new Book. The new Book becomes the active Book. Returns a Book object.
"""
return Book(impl=self.impl.add())
def active(self):
"""
Returns the active Book.
"""
return Book(impl=self.impl.active)
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))
else:
raise Exception('Book.caller() must not be called directly. Call through Excel or set a mock caller '
'first with Book.set_mock_caller().')
def parent(self):
"""
Returns the parent of the chart.
.. versionadded:: 0.9.0
"""
impl = self.impl.parent
if isinstance(impl, xlplatform.Book):
return Book(impl=self.impl.parent)
else:
return Sheet(impl=self.impl.parent)
def book(self):
"""Returns the Book of the specified Sheet. Read-only."""
return Book(impl=self.impl.book)
try:
impl = self.impl(name)
# on windows, samefile only available on Py>=3.2
if hasattr(os.path, 'samefile'):
throw = not os.path.samefile(impl.fullname, fullname)
else:
throw = (os.path.normpath(os.path.realpath(impl.fullname.lower())) != os.path.normpath(fullname.lower()))
if throw:
raise ValueError(
"Cannot open two workbooks named '%s', even if they are saved in different locations." % name
)
except KeyError:
impl = self.impl.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)
return Book(impl=impl)
def __eq__(self, other):
return isinstance(other, Book) and self.app == other.app and self.name == other.name
def run(self, *args):
args = [i.api
if isinstance(i, (App, Book, Sheet, Range, Shape, Chart, Picture, Name))
else i for i in args]
return self.app.impl.run(self.macro, args)