Skip to content

区域

单元格 A1 输入数组函数,给区域 A1:C2 赋值。运行脚本,输出区域 A1:B2 的值。

import xlwings as xw
from xlwings import func, script

@func
def two():
    return [[1, 2, 3], [4, 5, 6]]

@script
def print_two(book: xw.Book):
    sheet = book.sheets.active
    rg = sheet["A1:B2"]
    print(rg.value)
=TWO()
ABC
123
456
[[1, 2], [4, 5]]

单元格 A1 输入数组函数,给区域 A1:B3 赋值。

import xlwings as xw
from xlwings import func, script

@func
def two(rows, cols):
    result = []
    start = 1
    for i in range(rows):
        end = start + cols
        result.append(list(range(start, end)))
        start = end
    return result
=TWO(3, 2)
AB
12
34
56

脚本内容,运行脚本。

import xlwings as xw
from xlwings import script

@script
def update_1(book: xw.Book):
    sheet = book.sheets.active
    rows = 2
    cols = 3
    start = 1
    for i in range(rows):
        for j in range(cols):
            sheet[i, j].value = start
            start += 1
ABC
123
456

脚本内容,运行脚本。

import xlwings as xw
from xlwings import script

@script
def update_2(book: xw.Book):
    sheet = book.sheets.active
    rows = 2
    cols = 3
    start = 1
    rg = sheet[0, 0].resize(rows, cols)
    for i in rg:
        i.value = start
        start += 1
ABC
123
456

联系 math@baima.site