Openpyxl写入单个单元格

时间:2015-06-24 10:19:21

标签: python openpyxl

我使用openpyxl使用append函数在特定单元格中而不是在行中写入数据。 方法是什么?

我已尝试使用以下代码段:

variable = wb2s1.append(row = 4, column = 2)

variable = wb2s1.cell(row = 4, column = 2)

variable = wb2s1.cell(row = 4, column = 2).value

但没有任何作用。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

尝试:

wb2s1.append({'A' : 'This is A1', 'C' : 'This is C1'})

从源代码(http://openpyxl.readthedocs.org/en/latest/api/openpyxl.worksheet.html#openpyxl.worksheet.worksheet.Worksheet.append):

def append(self, list_or_dict):
    """Appends a group of values at the bottom of the current sheet.

    * If it's a list: all values are added in order, starting from the first column
    * If it's a dict: values are assigned to the columns indicated by the keys (numbers or letters)

    :param list_or_dict: list or dict containing values to append
    :type list_or_dict: list/tuple or dict

    Usage:

    * append(['This is A1', 'This is B1', 'This is C1'])
    * **or** append({'A' : 'This is A1', 'C' : 'This is C1'})
    * **or** append({0 : 'This is A1', 2 : 'This is C1'})

编辑:使用官方文档网站

difference = (cost_with - cost_without)/100

if b == 1:
    max_difference = difference
    currentRow = ['PV Size' + ' ' + str(size), difference]
else:
    currentRow.append(difference)

if max_difference > difference:
    max_difference = difference

if b==6:
   wb2s1.append(currentRow)

答案 1 :(得分:-1)

差异=(cost_with - cost_without)/ 100

        if b ==1:
            max_difference = difference

        if max_difference > difference:
            max_difference = difference

        wb2s1.append(['PV Size' + ' ' + str(size), difference])
        data = {difference}

        if b==6:
           wb2s1.append([data])