pythonpython-3.xpyxll

how to place multiple list on a specific cell co-ordinates using pyxll functions


Hii guys I am stuck in this bad situation I have to place multiple lists in excel at specific cell co-ordinates and cannot find any solution anywhere

Current implementation :

@xl_func("string key: object", macro=True)
def calc(key) -> object:
    result = [[1, 2], [3, 4]]
    from pyxll import xl_app, XLCell

    caller = xlfCaller()
    address = caller.address
    xl = xl_app()
    xl_range = xl.Range(address)

    one_below = xl_range.GetOffset(RowOffset=1, ColumnOffset=0)
    XLCell.from_range(one_below)
    cell = XLCell.from_range(one_below)
    cell.options(type="var", auto_resize=True).value = result
    return "123"

This code works perfectly for a single set of data. but now I want to add multiple such datasets on a specific cell co-ordinates . If possible something like follow :

@xl_func("string key: object", macro=True)
def calc(key) -> object:
    result = [[1, 2], [3, 4]]
    from pyxll import xl_app, XLCell

    caller = xlfCaller()
    address = caller.address
    xl = xl_app()
    xl_range = xl.Range(address)

    one_below = xl_range.GetOffset(RowOffset=1, ColumnOffset=0)
    XLCell.from_range(one_below)
    cell = XLCell.from_range(one_below)

    #This need to go between A1:A2 to B1:B2
    cell.options(type="var", auto_resize=True).value = result

    #This need to go between D1:D2 to E1:E2
    cell.options(type="var", auto_resize=True).value = result2

    #This need to go between F1:F2 to G1:G2
    cell.options(type="var", auto_resize=True).value = result3
    return "123"

Env: 
Python 3.8.6 32 bit 
Pyxll 5.0.5 32 bit

Solution

  • You can pass XLCell.from_range either a COM Range object or an address as a string.

    c = XLCell.from_range("A1")
    c.options(auto_resize=True).value = x
    

    Additionally, you can get a COM Range from an address using Application.Range, for example:

    xl = xl_app()
    r = xl.Range("A1")
    r.Value = x
    
    # or using XLCell
    c = XLCell.from_range(r)
    c.options(auto_resize=True).value = x
    

    If you need to you can also include the workbook and sheet in the address, eg "[Book1]Sheet1!A1".

    Please see https://www.pyxll.com/docs/api/classes.html#pyxll.XLCell.from_range for details.

    This FAQ article should also help https://support.pyxll.com/hc/en-gb/articles/360044507453-Writing-arrays-back-to-Excel