之前写过一个类似的,现在又要将一个xml文件转换为excel,这个比上次要容易,因为xml文件的结构很简单。
EXCEL效果:
原XML文件:
代码:
import re
import xlwt
f=open("partner_id.xml","r")
content=f.read()
f.close()
book=xlwt.Workbook()
sheet1=book.add_sheet("hi",cell_overwrite_ok=True)
tall_style=xlwt.easyxf("font:height 400")
title=["包名","渠道"]
for i in title:
sheet1.write(0,title.index(i),i)
channelList=re.findall("<PathDetail.*?</PathDetail>",content,re.S)
for j in channelList:
pname=re.findall("<pname>(.*?)</pname>",j,re.S)[0]
channel=re.findall("<pid>(.*?)</pid>",j,re.S)[0]
sheet1.write(channelList.index(j)+1,0,pname)
sheet1.write(channelList.index(j)+1,1,channel)
col0=sheet1.col(0)
col1=sheet1.col(1)
row0=sheet1.row(0)
col0.width=256*30
col1.width=256*20
row0.set_style(tall_style)
book.save("channel_id.xls")