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

yipeiwu_com6年前Python基础

本文实例讲述了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通过文件头判断文件类型

对于提供上传的服务器,需要对上传的文件进行过滤。 本文为大家提供了python通过文件头判断文件类型的方法,避免不必要的麻烦。 分享代码如下 import struct # 支...

Python利用matplotlib做图中图及次坐标轴的实例

Python利用matplotlib做图中图及次坐标轴的实例

图中图 准备数据 import matplotlib.pyplot as plt fig = plt.figure() x = [1, 2, 3, 4, 5, 6, 7] y =...

Python3实现购物车功能

Python3实现购物车功能

本文实例为大家分享了Python3实现购物车功能的具体代码,供大家参考,具体内容如下 购物车要求: 1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表...

python实现植物大战僵尸游戏实例代码

python实现植物大战僵尸游戏实例代码

开发思路 完整项目地址:https://github.com/371854496/... 觉得还OK的话,点下Star,作者不易,thank you! 实现方法 1.引入需要的模...

python黑魔法之编码转换

我们在使用其他语言的库做编码转换时,对于无法理解的字符,通常的处理也只有两种(或三种): 抛异常 替换成替代字符 跳过 但是在复杂的现实世界中,由于各种不靠谱,我们...