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
On executing i am able to get the data on my console for the Data_1.xls but the moment it enters into second set of for loop it gives me error as follows:-
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'
One more thing the first column of my both the excel sheets(Data_1 and Data_2.xls) are same. If in my newly generated excel sheet I want to combine and organize my data according to the respective first column values. What should be my course of action so that desired sorted excel sheet is obtained?
A typo!
Replace:
with