【问题标题】:Python Excel: How to add a cell value and a constant and then print back to the excelPython Excel:如何添加单元格值和常量,然后打印回 Excel
【发布时间】:2013-09-12 19:15:49
【问题描述】:

首先我需要知道单元格的值,如果单元格的值大于 1 或 1,那么它将打印到 Excel 表中。现在它的工作。

其次,如果单元格的值超过 1 个或 1 个,我想要的是它应该将列名与单元格值一起打印。例如:

while curr_row < total_rows:
    curr_row+=1
    curr_cols=-1
    while curr_cols<total_cols:
            curr_cols+=1
            cell_type=sheet1.cell_type(curr_row,curr_cols)
            print sheet1.cell_value(curr_row,curr_cols)
            if cell_type==1 or (cell_type==2 and sheet1.cell_value(curr_row,curr_cols)>=1):
                    Sheet.write(curr_row,curr_cols,sheet1.cell_value(curr_row,curr_cols))
                    print 'Current:', curr_cols   



0   0   0   0   0   0   0   0   0   0   0   0

0   0   0   0   0   0   0   0   1   0   0   0

0   1   0   0   0   0   0   0   0   0   0   0   

0   0   0   0   0   0   0   1   3   0   3   3

21  18  1   1   0   2   1   0   0   1   9   0   
 0  0   0   0   0   0   0   0   0   0   2   3

如果第 4 列中的值是 1 或大于 1。所以它应该打印 4:1。它的意思是4列,值为1。

【问题讨论】:

    标签: python excel python-2.7 xlrd xlwt


    【解决方案1】:

    这是您的代码的简化版本。另外,它会打印列索引和列值:

    import xlrd
    
    book_input = xlrd.open_workbook('input.xls')
    sheet_input = book_input.sheet_by_index(0)
    
    for row in xrange(sheet_input.nrows):
        for col in xrange(sheet_input.ncols):
            cell_type = sheet_input.cell_type(row, col)
            cell_value = sheet_input.cell_value(row, col)
            if cell_type == 1 or (cell_type == 2 and cell_value >= 1):
                print "%s:%s" % (col + 1, cell_value)
    

    希望这是你想要的。

    【讨论】:

    • 您好,非常感谢您的回答。是的,这就是我要找的。​​span>
    猜你喜欢
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多