我正在寻找删除或清除某些单元格内容的方法。在搜索之后,使用openpyxl似乎不是一件简单的事情。找到this similar question,建议将单元格设置为“无”。我这样做了,但是当我尝试附加数据时,它仍然将它附加到我设置为“无”的所有行的末尾。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)
在向其写入数据的工作簿中,我有冻结窗格和一些筛选器(显示所有数据)。对清除内容后如何强制从第3行追加有何建议?我正在使用python 2.7和openpyxl2.3。