You should define your workbook and sheet outside the loop:
def sh(dir):
a = xlwt.Workbook()
sheet = a.add_sheet('sheet1')
n = 0
for r,d,f in os.walk(dir):
for files in f:
if files.endswith(".xml"):
print files
dom=parse(os.path.join(r, files))
name = dom.getElementsByTagName('rev')
title = dom.getElementsByTagName('title')
sheet.write(n, 0, files)
sheet.write(n, 1, title[0].firstChild.nodeValue)
sheet.write(n, 2, name[0].firstChild.nodeValue)
n += 1
print title[0].firstChild.nodeValue
print name[0].firstChild.nodeValue
a.save('sha.xls')
Also, if you don't need to search for xml files inside subdirectories, consider switching to glob.glob() instead of using os.walk()
:
def sh(dir):
a = xlwt.Workbook()
sheet = a.add_sheet('sheet1')
n = 0
for f in glob.glob(os.path.join(dir, '*.xml')):
dom = parse(os.path.join(dir, f))
name = dom.getElementsByTagName('rev')
title = dom.getElementsByTagName('title')
sheet.write(n, 0, f)
sheet.write(n, 1, title[0].firstChild.nodeValue)
sheet.write(n, 2, name[0].firstChild.nodeValue)
n += 1
a.save('sha.xls')