xls读取和写入同一文件

时间:2017-10-10 08:16:57

标签: python xls xlrd xlwt

我需要xls read和xls写入同一个文件, 这里是代码,我可以读取和写入两个不同的文件,但是当我将读取和写入文件名改为同名时,所有其他工作表都会在保存的文件中删除。

""" Below is working code for XLS read and write"""
import xlwt , xlrd

try:
    r_filename = "1.xls"
    r_file_handle = xlrd.open_workbook(r_filename)
    r_file_sheet = r_file_handle.sheet_by_name("SPS-EXPORT")
except:
    print("Error read")

try:
    w_filename = "2.xls"
    w_file_handle = xlwt.Workbook(w_filename)
    w_file_sheet = w_file_handle.add_sheet('sheet1')
except:
    print("Error in write file")
    

index =0
column =9
for RowinXlS in range(0,len(r_file_sheet.col_values(0))):
    temp = funFind(str(r_file_sheet.cell_value(RowinXlS,3)))
    if(temp!=""):
        #print(r_file_sheet.cell_value(RowinXlS,3),temp)
        w_file_sheet.write(index,column,temp)
    index +=1
#    
w_file_handle.save(w_filename)

0 个答案:

没有答案