通过python在xls中进行行更新

时间:2014-08-21 08:31:11

标签: python excel python-2.7 xls

我想自动增加行数(此处为0到4)并填充一些值(此处为Name)。但我得到的最后一行值是用xls写的。

我必须做出哪些更改才能获得所有行(0到4的值)#。

import xlwt

row_no=0

def xls_write(row_no):

    book = xlwt.Workbook(encoding="utf-8")
    sheet1 = book.add_sheet("Result")
    Name = "Name is "
    sheet1.write(row_no, 0,Name)

    book.save("C:/Users/nirajk/Desktop/Row_checking.xls")

    print "Current row_no is : " , row_no


xls_write(row_no)
row_no+=1
xls_write(row_no)
row_no+=1
xls_write(row_no)
row_no+=1
xls_write(row_no)
row_no+=1
xls_write(row_no)

2 个答案:

答案 0 :(得分:0)

我想我明白你要做什么,但不熟悉xlwt库,所以如果它没有解决它,这应该让你朝着正确的方向前进。

编辑:扩展每个单元格中的变量文字

import xlwt


def xls_write(text, number_of_rows):
    book = xlwt.Workbook(encoding="utf-8")
    sheet1 = book.add_sheet("Result")

    for i in range(number_of_rows):
        sheet1.write(i, 0, text[i])
        print "Current row_no is : ", i
        print "Text is : ", text[i]

    book.save("C:/Users/nirajk/Desktop/Row_checking.xls")

words = ["cat", "dog", "horse", etc]
xls_write(words, len(words))

答案 1 :(得分:0)

回答这个问题 我使用了openpyxl模块

    import openpyxl

    Scene_Catalogue_file_path = "xls_file_path_and_name.xls"

    wb = openpyxl.load_workbook(Scene_Catalogue_file_path)
    print "All availabe_shhet : ", wb.get_sheet_names()

    sheet = wb.get_sheet_by_name('LVDS Recordings') # parsing only specific sheet Recordings

    print "B1", sheet['B1'].value

    call_no = 1
    while(sheet[str('B'+ str(call_no))].value != None):
        mention_recording = sheet['B' + str(call_no)].value
        Host_Vehicle_static_movie = sheet['C' + str(call_no)].value
        Lighting_Condition_poor_good = sheet['D' + str(call_no)].value
        Length  = sheet['I' + str(call_no)].value