读取xml文件,写入excel

  在上一篇 Python写xml文件已经将所有订单写入xml文件,这一篇我们把xml文件中的内容读出来,写入excel文件。

  输入xml格式:

 <?xml version="1.0" encoding="utf-8"?>
<orderlist>
<order>
<customer>姓名1</customer>
<phone>123456</phone>
<address>成都</address>
<count>2</count>
</order>
<order>
<customer>姓名2</customer>
<phone>234567</phone>
<address>成都</address>
<count>5</count>
</order>
<order>
<customer>姓名3</customer>
<phone>345678</phone>
<address>成都</address>
<count>1</count>
</order>
</orderlist>

  输出excel格式(这里按点餐次数进行的降序排序):

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAT4AAABfCAIAAAD+hTwsAAAKIklEQVR4nO1du67kNgz1V92v2yJVuv2YYJEmQJo0aRdpFjDuYottUuYPlGIQxys+RMkUadE8UOGRNRoeiseUX5rt2/d/smTJslzZfk4kEgti++XX36aWb9//jleS11olJK8tXZa8kteKJaWbvJLXkiWlm7yS15IlpZu8kteSRVm6f31593LZ29ub2W8Z8HoDiMELZReAl/1gaUr3ry/vv//xp7HLzr4z+y0bXhWjGCH+EF4GpNSk+9Ktl3RfnjJTb4a4FqkwvCCR2bx0pHvoNqWrTgpuL83LcnJkyQtSW0C6Z92mdHVJWZ472fBK6WqVq9KtdJvSVScFt5fmldLVKpekC3XrIt2oV2Ifcq5rUEKO17h0Ud16SdfMX168bKjlZSpFajeVLqXblO4kXjbUcrwW4rX801TneXLgW/yRQvyb4UiZ8Vr7kQwXl7mU5LVWCckrpZu8kteSZfqr9lmyZJlRpmfdEhH7+1dvE6YgeS2ElO4IQoZCSV5LwUG627Zt2yapHGhjg2YoNE29D5czeF5dNm//4ePHj9uPuP4TsBn/RZRXbyfDcTsJKd0RXJcu1UYY4pOgIt1t2z58+FA13rbt8+fPTbUIxcNsyHmldC9JV+j68mNMV5jhFx660nWnc0Ai3ab/qQYUO0mb4c4ZXjD24AZlJ19pgw7pDtxorqRbOf18bOZdcDevwVCgwhoGGQNHRi9cz7rHyBZMk81x5F3H7OVd3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xvEnIHINgJdNenTUMcd9DpBaY8aJQ9VBFbHmUdAtgy7vs+AiDu+Zg/sje3vPAoNw8NBok23y3cuzY01TXu3UHlC5swzNdW7oX39ctmHSretJK2XmIsLfrkEsXRj9lnqTeUrqK3bqjS7rUvKZKMGhesUTf/+sqSpeJQtxQWQawcSV17oTKiarsdULzGNG2u4XnSBdO9LrSQ1lFuodib75Khpkf0RCnSPF5GM5EKCJo9DDtB/Ac6cJtRrryLG0JO+kWOkyp+IMNmsFq40dJ1hUO7blZ79FNPXpSupIgRL9oD9O/xi5EbqT8he6iviLZpQXmZkMlRfmxhtdq02kqrFO6qFf5I/KtpQvT77B0i3hCwu/qTVC6ULzCnNI1gES6hR2LVbOuonSZYxtuX0p3qNsxPEq61VToHJmMb+GsaprVDXTfHLoyYRaGabWLAtVMzzkk5I9koLsGWKC81Fmj93V9A1QFzZtDcHSqvdAV7j7JVTJGEPJNlPIMXpIYg7EKtx8k3Uh4QohHQkheuUpGlixLlsy6I9gjHsVL8loKKd0RhAyFkryWQkp3BCFDoSSvpfAvtayFWBxAQS8AAAAASUVORK5CYII=" alt="" />

  工具库选择:beautifulsoup+lxml用于处理xml文件(当然也可直接使用lxml), xlsxwriter用于写excel文件。

  思路也比较简单:找出xml文件中的每一个<order>,然后进行降序排序,再将每一个order信息写入excel文件,每个order一行。

  代码如下:

 # coding: utf-8

 import bs4
import xlsxwriter # 读取xml文件,写入excel
def xmlToExcel(file_xml, file_excel):
# 打开xml文件,并以此创建一个bs对象
xml = open(file_xml, 'r')
doc = bs4.BeautifulSoup(xml, 'xml') # 创建一个excel文件,并添加一个sheet,命名为orders
workbook = xlsxwriter.Workbook(file_excel)
sheet = workbook.add_worksheet('orders') # 设置粗体
bold = workbook.add_format({'bold': True}) # 先在第一行写标题,用粗体
sheet.write('A1', u'姓名', bold)
sheet.write('B1', u'电话', bold)
sheet.write('C1', u'点餐次数', bold)
sheet.write('D1', u'地址', bold) # 筛选出所有的<order>,这里使用的是CSS选择器
order = doc.select('order') # 以每个order的count元素,对order进行降序排序
sort_key = lambda a: int(a.count.text)
order.sort(key=sort_key, reverse=True) # 行号,具体订单信息从第二行开始
row = 2
# 将每一个订单写入excel
for x in order:
# 提取出具体信息
name = x.customer.text
phone = x.phone.text
cnt = x.count.text
addr = x.address.text # 将具体信息写入excel
sheet.write('A%d' % row, name)
sheet.write('B%d' % row, phone)
sheet.write('C%d' % row, cnt)
sheet.write('D%d' % row, addr) row += 1 # 关闭文件
xml.close()
workbook.close() # 测试代码
if __name__ == '__main__':
file1 = 'hh.xml'
file2 = 'hehe.xlsx' xmlToExcel(file1, file2)
上一篇:perl6 中将 字符串 转成十六进制


下一篇:php更新修改excel中的内容例子