0

既存のExcelから新しいExcelを作成しています。3 Sheets私の古いエクセルには、、、と言うものfirstsecondありthirdます。最初のシートの内容を、ある条件に基づいた新しいExcelで正常にコピーできます。つまり、以前のコードwhile index < 3 :は完全に正常に機能します。シートの場合は、そのままコピーしたいと思いsecondます。しかし、シートのコピーを開始すると、エラーが発生します:-thirdoriginal excel sheetsecond

sheet1 = w.add_sheet('Info')
File "/usr/local/lib/python2.7/site-packages/xlwt/Workbook.py", line 331, in add_sheet
raise Exception("duplicate worksheet name %r" % sheetname) 

私のコードは次のとおりです:-

newexcel = "newexcel.xls"
    count = 0
    wb = xlrd.open_workbook('/home/sam/myexcel.xls')
    w = Workbook()
    sheet = w.add_sheet('Input_Resource')
    index = 0
    s = wb.sheet_by_index(index)
    if index < 1 :
        index =+ 1
        for row in range(s.nrows):
            coln =0
            val = s.cell(row,coln).value
            if val in MissingId :
                mylist.append(val)
                count += 1
            else:
                for col in range(s.ncols):
                    val = s.cell(row,col).value
                    sheet.write(row-count,col,val)
    while index < 3 :
        if index == 1 :
            sheet1 = w.add_sheet('Info')
        else :
            sheet2 = w.add_sheet('Sheet3')

        s = wb.sheet_by_index(index)
        index =+ 1
        for row in range(s.nrows):
            coln =0
            val = s.cell(row,coln).value
            for col in range(s.ncols):
                val = s.cell(row,col).value
                if index == 1:
                    sheet1.write(row,col,val)
                else :
                    sheet2.write(row,col,val)
w.save(newexcel)

ヘルプやヒントをいただければ幸いです:)

4

1 に答える 1

0

重複シートをチェックするためのいくつかのアイデアの下:

import xlwt

wb = xlwt.Workbook()
#Adding 2 sheets
ws1 = wb.add_sheet('One')
ws2 = wb.add_sheet('Two')

sheets = wb._Workbook__worksheets #Getting sheets list
sheet_names = [] #List for shhets names
for sheet in sheets:
     sheet_names.append(
         sheet.get_name() #getting sheet name
         ) 
print sheet_names
print True if u"One" in sheet_names else False #check for our name
wb.save('test.xls')

結果:

$python simple.py 
[u'One', u'Two']
True
于 2013-10-02T12:02:36.600 に答える