溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python實(shí)現(xiàn)將Excel轉(zhuǎn)換成xml的方法示例

發(fā)布時(shí)間:2020-09-06 16:26:21 來(lái)源:腳本之家 閱讀:255 作者:陽(yáng)老板 欄目:開(kāi)發(fā)技術(shù)

本文實(shí)例講述了Python實(shí)現(xiàn)將Excel轉(zhuǎn)換成xml的方法。分享給大家供大家參考,具體如下:

最近寫了個(gè)小工具 用于excel轉(zhuǎn)成xml

直接貼代碼吧:

#coding=utf-8
import xlrd
import datetime
import time
import sys
import xml.dom.minidom
import os
print sys.getdefaultencoding()
reload(sys)       #就是這么坑爹,否則下面會(huì)報(bào)錯(cuò)
sys.setdefaultencoding('utf-8') #py默認(rèn)是ascii。。要設(shè)成utf8
#excel中 數(shù)據(jù)格式如下:
# 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的外部庫(kù)
    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')   #將時(shí)間轉(zhuǎn)成時(shí)間戳
  return stime
def excel_table_byindex(file, colnnameindex=0, by_index=0):
  data = open_excel(file)     #打開(kāi)excel
  table = data.sheets()[by_index]
  nrows = table.nrows
  ncols = table.ncols
  doc = xml.dom.minidom.Document()  #打開(kāi)xml對(duì)象
  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) #生成節(jié)點(diǎn)
    stime = table.cell(nrow, 1).value  #第二列的值
    stime = stime.strip()    #去除空格..excel數(shù)據(jù)里 經(jīng)常會(huì)無(wú)意有蛋疼的多余空格
    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)  #純文本節(jié)點(diǎn)
    exxend = doc.createTextNode(send)
    item.appendChild(exxbgn)      #加入樹(shù)中
    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]))) #設(shè)置節(jié)點(diǎn)屬性
    # 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文件路徑

關(guān)于xlrd 可以在cmd里pip install xlrd來(lái)安裝

PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉(zhuǎn)換工具:
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

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結(jié)》、《Python操作Excel表格技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI