1
 import xlwt
 import xlrd
 import os.path
 import os
 print os.path.abspath(os.curdir) 

#Create workbook and worksheet

wbk = xlwt.Workbook()
sheet = wbk.add_sheet('Data_1')#name of the sheet in which new data is there

row = 0

f = xlrd.open_workbook('Data_1.xls')#workbook from where data is extracted
g = xlrd.open_workbook('Data_2.xls')#workbook from where data is extracted

sheet1 = f.sheet_by_index(0)
sheet2 = g.sheet_by_index(0)

for i in xrange(16):
temp = sheet1.row_values(i)
print temp
col = 0
for element in temp:        
    sheet.write(i,col,element)
    col += 1
i += 1

for j in xrange(16):
temp = sheet2.row_values(j)
print temp
col = 4
for elements in temp:
    sheet.write(j,col.element)
    col +=1
j+=1
 wbk.save('DATA.xls') #excel sheet name when saved in the folder

在执行时,我能够在控制台上获取 Data_1.xls 的数据,但是当它进入第二组 for 循环时,它会给我如下错误:-

Traceback (most recent call last):
File "C:/Python26/combining2ExcelSheets.py", line 34, in <module>
sheet.write(j,col.element)
AttributeError: 'int' object has no attribute 'element'

还有一件事我的两个excel表(Data_1和Data_2.xls)的第一列是相同的。如果在我新生成的 Excel 表中,我想根据各自的第一列值组合和组织我的数据。我应该采取什么行动才能获得所需的排序 Excel 表?

4

1 回答 1

4

一个错字!

代替:

sheet.write(j,col.element)

sheet.write(j,col,element)
于 2012-06-22T06:50:37.903 回答