Openpyxl删除细胞/清除内容

时间:2015-09-02 09:44:57

标签: python excel openpyxl

我一直在寻找一种删除或清除某些单元格内容的方法。搜索之后,看起来使用openpyxl并不是一件简单的事情。找到this similar question建议将单元格设置为无。我这样做了,但是当我尝试追加数据时,它仍会将其附加到我设置为None的所有行的末尾。

def clear_sheet(this_sheet):
    '''
    Clear all cells starting from third row. we keep our header and
    replaces other rows content by None
    '''
    dst_wb=openpyxl.load_workbook(dest_filename, read_only=False, keep_vba=True)
    dst_ws=dst_wb.get_sheet_by_name(this_sheet)
    #We kee the header located in row 2, and set the rest to None
    if dst_ws.cell('A3').value is not None: #check if already cleared content
        for row in dst_ws.iter_rows(row_offset=2):
            for cell in row:
                cell.value=None
    dst_wb.save(dest_filename)



def copy_data(searchMilestone):
    '''
    gets data from the sequence file
    '''
    dst_wb=openpyxl.load_workbook(dest_filename, read_only=False, keep_vba=True)
    dst_ws=dst_wb.get_sheet_by_name(sequence_sheet)
    data =[]
    print dst_ws.max_row #here should return 3, but it seems it is counting all the cells we set to None
    for i in xrange(sequenceWs.nrows):
        #this reading part is done using xlrd module
        milestone=sequenceWs.cell(i,1).value
        execution=sequenceWs.cell(i,2).value
        system=sequenceWs.cell(i,16).value
        if searchMilestone in milestone and 'WC' in execution:
            #copy some data
            line=[data, data, data]
            dst_ws.append(line)
    dst_wb.save(dest_filename)

在我写入数据的工作簿中,我有冻结窗格和一些过滤器(显示所有数据)。有什么关于如何在清除内容后强制从row3追加的建议?我使用的是python 2.7和openpyxl 2.3。

0 个答案:

没有答案