david98031

convert XML to XLS

Jan 29th, 2019
126
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import xlwt
  2. import xml.etree.ElementTree as ET
  3. import sys
  4. import xml
  5.  
  6. pmtflds = ['PmtType', 'PmtStatus', 'PrcDate']
  7. flds = ['PmtID', 'Name1', 'ValueDate', 'CurAmt', 'CurCode', 'Error']
  8.  
  9. wb = xlwt.Workbook()
  10. ws = wb.add_sheet('TranACK')
  11.  
  12. wrapstyle = xlwt.XFStyle()
  13. wrapstyle.alignment.wrap = 1
  14.  
  15. for (i, n) in enumerate(pmtflds + flds):
  16.     ws.write(0, i, n)
  17.  
  18. tree = ET.parse(sys.argv[1])
  19. root = tree.getroot()
  20.  
  21. width = 0
  22.  
  23. #ET.dump(root)
  24. row = 1
  25. for ack in root:
  26.     for item in ack:
  27.         if item.tag != 'TranACK':
  28.             continue
  29.         for (i, n) in enumerate(pmtflds):
  30.             ws.write(row, i, ack.attrib[n])
  31.         for (fldnum, fld) in enumerate(flds):
  32.             print('searching for {}'.format(fld))
  33.             val = '\n'.join([i.text.strip() for i in item.findall(fld) if i != None])
  34.             if '\n' in val:
  35.                 for s in val.split('\n'):
  36.                     if len(s) > width:
  37.                         width = len(s)
  38.                 ws.write(row, fldnum+len(pmtflds), val, wrapstyle)
  39.             else:
  40.                 ws.write(row, fldnum+len(pmtflds), val)
  41.         print('')
  42.         row += 1
  43.  
  44. ws.col(8).width = width * 256
  45. wb.save('wfack.xls')
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×