Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __repr__(self):
return "".format(self.name,
Sheet(self.sheet_name_or_index).name,
xlplatform.get_workbook_name(self.xl_workbook))
def clear_contents(self):
"""
Clears the content of a Range but leaves the formatting.
"""
xlplatform.clear_contents_range(self.xl_range)
self.asarray = kwargs.get('asarray', False) # Return Data as NumPy Array
self.strict = kwargs.get('strict', False) # Stop table/horizontal/vertical at empty cells that contain formulas
self.atleast_2d = kwargs.get('atleast_2d', False) # Force data to be list of list or a 2d numpy array
# Get sheet
if sheet_name_or_index:
self.xl_sheet = xlplatform.get_worksheet(self.xl_workbook, sheet_name_or_index)
else:
self.xl_sheet = xlplatform.get_active_sheet(self.xl_workbook)
# Get xl_range object
if range_address:
self.row1 = xlplatform.get_first_row(self.xl_sheet, range_address)
self.col1 = xlplatform.get_first_column(self.xl_sheet, range_address)
self.row2 = self.row1 + xlplatform.count_rows(self.xl_sheet, range_address) - 1
self.col2 = self.col1 + xlplatform.count_columns(self.xl_sheet, range_address) - 1
if 0 in (self.row1, self.col1, self.row2, self.col2):
raise IndexError("Attempted to access 0-based Range. xlwings/Excel Ranges are 1-based.")
self.xl_range = xlplatform.get_range_from_indices(self.xl_sheet, self.row1, self.col1, self.row2, self.col2)
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)
"""
Creates a new Excel file with the xlwings VBA module already included. This method must be called from an
interactive Python shell::
>>> Workbook.open_template()
.. versionadded:: 0.3.3
"""
this_dir = os.path.abspath(os.path.dirname(inspect.getfile(inspect.currentframe())))
template_file = 'xlwings_template.xltm'
try:
os.remove(os.path.join(this_dir, '~$' + template_file))
except OSError:
pass
xlplatform.open_template(os.path.realpath(os.path.join(this_dir, template_file)))
self.index = kwargs.get('index', True) # Set DataFrame with index
self.header = kwargs.get('header', True) # Set DataFrame with header
self.asarray = kwargs.get('asarray', False) # Return Data as NumPy Array
self.strict = kwargs.get('strict', False) # Stop table/horizontal/vertical at empty cells that contain formulas
self.atleast_2d = kwargs.get('atleast_2d', False) # Force data to be list of list or a 2d numpy array
# Get sheet
if sheet_name_or_index:
self.xl_sheet = xlplatform.get_worksheet(self.xl_workbook, sheet_name_or_index)
else:
self.xl_sheet = xlplatform.get_active_sheet(self.xl_workbook)
# Get xl_range object
if range_address:
self.row1 = xlplatform.get_first_row(self.xl_sheet, range_address)
self.col1 = xlplatform.get_first_column(self.xl_sheet, range_address)
self.row2 = self.row1 + xlplatform.count_rows(self.xl_sheet, range_address) - 1
self.col2 = self.col1 + xlplatform.count_columns(self.xl_sheet, range_address) - 1
if 0 in (self.row1, self.col1, self.row2, self.col2):
raise IndexError("Attempted to access 0-based Range. xlwings/Excel Ranges are 1-based.")
self.xl_range = xlplatform.get_range_from_indices(self.xl_sheet, self.row1, self.col1, self.row2, self.col2)
wkb : Workbook object, default Workbook.current()
Defaults to the Workbook that was instantiated last or set via ``Workbook.set_current()``.
.. versionadded:: 0.5.0
"""
xl_workbook = Workbook.get_xl_workbook(wkb)
if isinstance(sheet, Sheet):
sheet = sheet.index
if sheet is None:
sheet = xlplatform.get_worksheet_index(xlplatform.get_active_sheet(xl_workbook))
if name:
if name in xlplatform.get_shapes_names(xl_workbook, sheet):
raise ShapeAlreadyExists('A shape with this name already exists.')
if sys.platform.startswith('darwin') and xlplatform.get_major_app_version_number(xl_workbook) >= 15:
# Office 2016 for Mac is sandboxed. This path seems to work without the need of granting access explicitly
xlwings_picture = os.path.expanduser("~") + '/Library/Containers/com.microsoft.Excel/Data/xlwings_picture.png'
shutil.copy2(filename, xlwings_picture)
filename = xlwings_picture
# Image dimensions
im_width, im_height = None, None
if width is None or height is None:
if Image:
im = Image.open(filename)
im_width, im_height = im.size
if width is None:
def top(self, value):
xlplatform.set_shape_top(self, value)
def __init__(self, *args, **kwargs):
super(Picture, self).__init__(*args, **kwargs)
self.xl_picture = xlplatform.get_picture(self)
self.index = xlplatform.get_picture_index(self)
def __init__(self, *args, **kwargs):
super(Chart, self).__init__(*args, **kwargs)
# Get xl_chart object
self.xl_chart = xlplatform.get_chart_object(self.xl_workbook, self.sheet_name_or_index, self.name_or_index)
self.index = xlplatform.get_chart_index(self.xl_chart)
# Chart Type
chart_type = kwargs.get('chart_type')
if chart_type:
self.chart_type = chart_type
# Source Data
source_data = kwargs.get('source_data')
if source_data:
self.set_source_data(source_data)