簡體   English   中英

Python Excel:如何添加一個單元格值和一個常量,然后打印回excel

[英]Python Excel: How to add a cell value and a constant and then print back to the excel

首先,我需要知道單元格的值,如果單元格的值大於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。

這是您的代碼的簡化版本。 另外,它會打印列索引和列值:

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)

希望這就是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM