How to use the xlwings.Book function in xlwings

To help you get started, we’ve selected a few xlwings examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xlwings / xlwings / scripts / build_rest_api_docs.py View on Github external
sheet1 = wb1.sheets[0]

sheet1['A1'].value = [[1.1, 'a string'], [dt.datetime.now(), None]]
sheet1['A1'].formula = '=1+1.1'
chart = sheet1.charts.add()
chart.set_source_data(sheet1['A1'])
chart.chart_type = 'line'


pic = os.path.abspath(os.path.join('..', 'xlwings', 'tests', 'sample_picture.png'))
sheet1.pictures.add(pic)
wb1.sheets[0].range('B2:C3').name = 'Sheet1!myname1'
wb1.sheets[0].range('A1').name = 'myname2'
wb1.save('Book1.xlsx')
wb1 = xw.Book('Book1.xlsx')  # hack as save doesn't return the wb properly
app1.activate()


def generate_get_endpoint(endpoint):
    docs = []
    docs.append('.. http:get:: ' + endpoint.replace('path:', ''))
    docs.append('')
    url = (BASE_URL +
           endpoint.replace('', str(wb1.app.pid))
                   .replace('', wb1.name)
                   .replace('', '0')
                   .replace('', 'myname2')
                   .replace('', 'myname1')
                   .replace('', 'sheet1')
                   .replace('', '0')
                   .replace('', wb1.name)
github lionheart / python-harvest / examples / xlwings / accounting.py View on Github external
def delete_columns(sheet_to_delete_from, columns_to_keep, sheet_rename, delete=True):
    if not isinstance(columns_to_keep, list):
        return

    # get the worksheet we are interested in (providing it exists)
    wb = xw.Book.caller()
    sheets = xw.sheets
    working_sheet = None
    for sheet in sheets:
        if sheet.name == sheet_to_delete_from:
            working_sheet = sheet

    if working_sheet is not None:
        columns = working_sheet.used_range.columns.count

        # excel column references are 1 based
        columns += 1
        length = 0
        column_ordinals_to_delete = []

        for col in range(1, columns):
            length += 1
github xlwings / xlwings / scripts / build_addins.py View on Github external
def build_addins():
    # transform code for addin use
    with open(os.path.join(par_dir, "xlwings", "xlwings.bas"), "r") as vba_module, \
         open(os.path.join(this_dir, "xlwings_addin.bas"), "w") as vba_addin:
        content = vba_module.read().replace("ThisWorkbook", "ActiveWorkbook")
        content = content.replace('Attribute VB_Name = "xlwings"', 'Attribute VB_Name = "xlwings_addin"')
        vba_addin.write(content)

    # create addin workbook
    wb = Book()

    # remove unneeded sheets
    for sh in list(wb.sheets.api)[1:]:
        sh.Delete()

    # rename vbproject
    wb.api.VBProject.Name = "xlwings"
    
    # import modules
    wb.api.VBProject.VBComponents.Import(os.path.join(this_dir, "xlwings_addin.bas"))
    
    # save to xla and xlam
    wb.api.IsAddin = True
    wb.app.display_alerts = False
    # wb.api.SaveAs(os.path.join(this_dir, "xlwings.xla"), FileFormat.xlAddIn)
    wb.api.SaveAs(os.path.join(this_dir, "xlwings.xlam"), FileFormat.xlOpenXMLAddIn)
github xlwings / simulation-demo / xlwings_app.py View on Github external
def run_simulation():
    sht = xw.Book.caller().sheets[0]

    # User Inputs
    num_simulations = sht.range('E3').options(numbers=int).value
    time_horizon = sht.range('E4').value
    num_timesteps = sht.range('E5').options(numbers=int).value
    dt = time_horizon / num_timesteps  # Length of time period
    vol = sht.range('E7').value
    mu = np.log(1 + sht.range('E6').value)  # Drift
    starting_price = sht.range('E8').value
    perc_selection = [5, 50, 95]

    # Excel: clear output, write out initial values of percentiles/sample path and set chart source and x-axis values
    sht.range('O2').expand().clear_contents()
    sht.range('P2').value = [starting_price, starting_price, starting_price, starting_price, perc_selection]
    sht.charts['Chart 5'].set_source_data(sht.range((1, 15), (num_timesteps + 2, 19)))
    sht.range('O2').value = np.round(np.linspace(0, time_horizon, num_timesteps + 1).reshape(-1, 1), 2)
github xlwings / xlwings / app.py View on Github external
def names(path):
    workbook = xw.Book(path)
    return jsonify(value=[
        {
            'name': name.name,
            'type': "Range",
            'value': name.refers_to[1:],
            'visible': True
            
        } 
        for name in workbook.names
    ])
github xlwings / xlwings / scripts / build_standalone_files.py View on Github external
content = content.replace('Attribute VB_Name = "License"', "")
    content = content.replace("Attribute VB_Name", "\n'Attribute VB_Name")
    content = content.replace("Option Explicit", "")
    xw_module.seek(0, 0)
    xw_module.write('Attribute VB_Name = "xlwings"\n')
    xw_module.write("'Version: {}\n".format(version))

    xw_module.write(content)

os.remove("temp.bas")

# update standalone files
standalone_files = ['../xlwings/quickstart_standalone_mac.xlsm', '../xlwings/quickstart_standalone_win.xlsm']

for f in standalone_files:
    wb = xw.Book(os.path.abspath(f))
    try:
        wb.api.VBProject.VBComponents.Remove(wb.api.VBProject.VBComponents("xlwings"))
    except pywintypes.com_error:
        pass
    wb.api.VBProject.VBComponents.Import(os.path.abspath(os.path.join(this_dir, 'xlwings.bas')))
    wb.save()

for f in standalone_files:
    xw.Book(os.path.abspath(f)).close()