import os
import sys
import win32com.client
import codecs

fln = sys.argv[1].replace(os.path.sep, '/')
xml = fln + '.xml'

eao = win32com.client.Dispatch('Excel.Application')
eao.Visible = False
eao.DisplayAlerts = False

dic = []
wbo = eao.Workbooks.Open(fln)
for i in range(0, wbo.WorkSheets.Count) :
    wso = wbo.WorkSheets(i + 1)
    usr = wso.UsedRange
    tags = []
    for j in wso.Comments :
        t = j.Text()
        if not t : continue
        tag = ['<comment r="', str(j.Parent.Row), '"']
        tag.extend([' c="', str(j.Parent.Column), '">'])
        tag.extend([t, '</comment>'])
        tags.append(''.s.join(tag))
    tmp = {'wsname' : wso.Name}
    tmp['r_top'] = usr.Row
    tmp['c_top'] = usr.Column
    tmp['data'] = usr.Value
    if tmp['data'] is None : continue
    if len(tags) > 0 : tmp['comm'] = crf.join(tags)
    dic.append(tmp)
    if i == wbo.WorkSheets.Count : break
wbo.Close()
eao.Quit()

tags = [unicode('<b path="' + fln + '">')]
for w in dic :
    tags.append(unicode('<s name="' + w['wsname'] + '">'))
    if 'comm' in w : tags.append(w['comm'])
    nrow = w['r_top']
    for r in w['data'] :
        ncol = w['c_top']
        for c in r :
            if c is not None :
                tag = ['<w r="', str(nrow), '"']
                tag.extend([' c="', str(ncol), '">'])
                tag.extend([unicode(c), '</w>'])
                tags.append(''.join(tag))
            ncol = ncol + 1
        nrow = nrow + 1
    tags.append('</s>')
tags.append('</b>')
output = '\n'.join(tags)

f = codecs.open(xml, 'w', 'utf8') # change from 'sjis'
f.write(output)
f.close