您的位置:首页 > 编程语言 > Python开发

Python实现将Excel转换成xml的方法示例

2018-08-25 14:25 3759 查看

本文实例讲述了Python实现将Excel转换成xml的方法。分享给大家供大家参考,具体如下:

最近写了个小工具 用于excel转成xml

直接贴代码吧:

#coding=utf-8
import xlrd
import datetime
import time
import sys
import xml.dom.minidom
import os
print sys.getdefaultencoding()
reload(sys)       #就是这么坑爹,否则下面会报错
sys.setdefaultencoding('utf-8') #py默认是ascii。。要设成utf8
#excel中 数据格式如下:
# UID     第四天
# 1579880025 10:00-13:30
# 1677982825 10:00-12:00
# 1704410718 10:00-12:00
# 83713892  10:00-12:00
# 1546551561 10:00-12:00
# 1298790776 10:00-12:00
def open_excel(file):
try:
data = xlrd.open_workbook(file) #xlrd 操作excel的外部库
return data
except Exception, e:
print str(e)
bgntm = '2017-05-18_'
def get_time_t(stime):
stime = bgntm + stime + ':00'
# return time.strptime(stime, '%Y-%m-%d %H:%M:%S')   #将时间转成时间戳
return stime
def excel_table_byindex(file, colnnameindex=0, by_index=0):
data = open_excel(file)     #打开excel
table = data.sheets()[by_index]
nrows = table.nrows
ncols = table.ncols
doc = xml.dom.minidom.Document()  #打开xml对象
xmain = doc.createElement('main')
doc.appendChild(xmain)
for nrow in range(0, nrows):    #遍历每一行
if nrow == 0:
continue
uid = table.cell(nrow, 0).value   #取值..第一列
item = doc.createElement('%d'%uid) #生成节点
stime = table.cell(nrow, 1).value  #第二列的值
stime = stime.strip()    #去除空格..excel数据里 经常会无意有蛋疼的多余空格
listT = stime.split('-')     #按 -分割字符串
# sbgn = 'bgn = %d'%time.mktime(get_time_t(listT[0]))
sbgn = 'bgn = '+get_time_t(listT[0])
print 'uid=%d'%uid
print 'bgn:'+sbgn
send = 'end = '+get_time_t(listT[1])
# send = 'end = %d'%time.mktime(get_time_t(listT[1]))
print 'end:'+send
exxbgn = doc.createTextNode(sbgn)  #纯文本节点
exxend = doc.createTextNode(send)
item.appendChild(exxbgn)      #加入树中
item.appendChild(exxend)
# ebgn = doc.createElement('bgn')
# eend = doc.createElement('bgn')
# item.appendChild(ebgn)
# item.appendChild(eend)
# item.setAttribute('bgn', '%d'%time.mktime(get_time_t(listT[0]))) #设置节点属性
# item.setAttribute('end', '%d'%time.mktime(get_time_t(listT[1])))
# for lt in listT:
# print time.mktime(get_time_t(lt))
xmain.appendChild(item)
f = open('G:/testPro/py/exceltoxml/day.xml', 'w')    #xml文件输出路径
f.write(doc.toprettyxml())
f.close()
excel_table_byindex('G:/testPro/py/exceltoxml/day.xlsx')    #excel文件路径

关于xlrd 可以在cmd里

pip install xlrd
来安装

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XML:
http://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python操作xml数据技巧总结》、《Python操作Excel表格技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python Excel xml