Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Defaults to the Workbook that was instantiated last or set via ``Workbook.set_current()``.
Examples
--------
>>> Sheet.all()
[, ]
>>> [i.name.lower() for i in Sheet.all()]
['sheet1', 'sheet2']
>>> [i.autofit() for i in Sheet.all()]
.. versionadded:: 0.2.3
"""
xl_workbook = Workbook.get_xl_workbook(wkb)
sheet_list = []
for i in range(1, xlplatform.count_worksheets(xl_workbook) + 1):
sheet_list.append(Sheet(i, wkb=wkb))
return sheet_list
def __call__(self, name_or_index):
if isinstance(name_or_index, Sheet):
return name_or_index
else:
return Sheet(impl=self.impl(name_or_index))
def __repr__(self):
return "".format(self.name,
Sheet(self.sheet_name_or_index).name,
xlplatform.get_workbook_name(self.xl_workbook))
return Book(impl=impl)
class Sheets(Collection):
"""
A collection of all :meth:`sheet ` objects:
>>> import xlwings as xw
>>> xw.sheets # active book
Sheets([, ])
>>> xw.Book('Book1').sheets # specific book
Sheets([, ])
.. versionadded:: 0.9.0
"""
_wrap = Sheet
@property
def active(self):
"""
Returns the active Sheet.
"""
return Sheet(impl=self.impl.active)
def __call__(self, name_or_index):
if isinstance(name_or_index, Sheet):
return name_or_index
else:
return Sheet(impl=self.impl(name_or_index))
def __delitem__(self, name_or_index):
self[name_or_index].delete()
def sheet(self):
"""
Returns the Sheet object to which the Range belongs.
.. versionadded:: 0.9.0
"""
return Sheet(impl=self.impl.sheet)
>>> new_sheet.index
4
.. versionadded:: 0.2.3
"""
xl_workbook = Workbook.get_xl_workbook(wkb)
if before is None and after is None:
after = Sheet(Sheet.count(wkb=wkb), wkb=wkb)
elif before:
before = Sheet(before, wkb=wkb)
elif after:
after = Sheet(after, wkb=wkb)
if name:
if name.lower() in [i.name.lower() for i in Sheet.all(wkb=wkb)]:
raise Exception('That sheet name is already taken.')
else:
xl_sheet = xlplatform.add_sheet(xl_workbook, before, after)
xlplatform.set_worksheet_name(xl_sheet, name)
return cls(name, wkb)
else:
xl_sheet = xlplatform.add_sheet(xl_workbook, before, after)
return cls(xlplatform.get_worksheet_name(xl_sheet), wkb)
def active(self):
"""
Returns the active Sheet.
"""
return Sheet(impl=self.impl.active)
def __init__(self, *args, **kwargs):
# Arguments
if len(args) == 1 and isinstance(args[0], string_types):
sheet_name_or_index = None
range_address = args[0]
elif len(args) == 1 and isinstance(args[0], tuple):
sheet_name_or_index = None
range_address = None
self.row1 = args[0][0]
self.col1 = args[0][1]
self.row2 = self.row1
self.col2 = self.col1
elif (len(args) == 2
and isinstance(args[0], (numbers.Number, string_types, Sheet))
and isinstance(args[1], string_types)):
if isinstance(args[0], Sheet):
sheet_name_or_index = args[0].index
else:
sheet_name_or_index = args[0]
range_address = args[1]
elif (len(args) == 2
and isinstance(args[0], (numbers.Number, string_types, Sheet))
and isinstance(args[1], tuple)):
if isinstance(args[0], Sheet):
sheet_name_or_index = args[0].index
else:
sheet_name_or_index = args[0]
range_address = None
self.row1 = args[1][0]
self.col1 = args[1][1]
Examples
--------
>>> Sheet.add() # Place at end with default name
>>> Sheet.add('NewSheet', before='Sheet1') # Include name and position
>>> new_sheet = Sheet.add(after=3)
>>> new_sheet.index
4
.. versionadded:: 0.2.3
"""
xl_workbook = Workbook.get_xl_workbook(wkb)
if before is None and after is None:
after = Sheet(Sheet.count(wkb=wkb), wkb=wkb)
elif before:
before = Sheet(before, wkb=wkb)
elif after:
after = Sheet(after, wkb=wkb)
if name:
if name.lower() in [i.name.lower() for i in Sheet.all(wkb=wkb)]:
raise Exception('That sheet name is already taken.')
else:
xl_sheet = xlplatform.add_sheet(xl_workbook, before, after)
xlplatform.set_worksheet_name(xl_sheet, name)
return cls(name, wkb)
else:
xl_sheet = xlplatform.add_sheet(xl_workbook, before, after)
return cls(xlplatform.get_worksheet_name(xl_sheet), wkb)