Guest User

py-export-rows.py

a guest
Oct 27th, 2019
128
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import sys
  3. import xml.etree.ElementTree as ET
  4. import pandas as pd
  5. import weasyprint
  6.  
  7. html = '''
  8. <html>
  9.  <head></head>
  10.  <style>
  11.  thead {{
  12.    background-color: #70ad46;
  13.    color: #ffffff;
  14.    font-weight: bold;
  15.  }}
  16.  table {{
  17.      border-collapse: collapse;
  18.      font-size: 14px;
  19.  }}
  20.  table th:nth-child(3),
  21.  table td:nth-child(3),
  22.  table th:nth-child(5),
  23.  table td:nth-child(5),
  24.  table th:nth-child(6),
  25.  table td:nth-child(6),
  26.  table th:nth-child(7),
  27.  table td:nth-child(7) {{
  28.    font-size: 12px;
  29.  }}
  30.  table td, table th {{
  31.    border: 1px #bcd9a9 solid;
  32.    padding: 4px;
  33.  }}
  34.  tbody tr:nth-child(odd) {{
  35.    background-color: #e2efdb;
  36.  }}
  37.  </style>
  38.  <body>
  39.    {table}
  40.  </body>
  41. </html>
  42. '''
  43.  
  44. rows = []
  45. cols = ['docid', 'ordner', 'hauptordner', 'bemerkung', 'status', 'revision', 'dokumentenart', 'Datum', 'bu-monat', 'bu-jahr', 'firma']
  46. print_cols = ['docid', 'hauptordner', 'bemerkung', 'dokumentenart', 'bu-monat', 'bu-jahr', 'firma']
  47.  
  48. tree = ET.parse('export.xml')
  49. root = tree.getroot()
  50.  
  51. for node in root:
  52.     if not node.attrib.get('docid'):
  53.         continue
  54.  
  55.     rows.append({
  56.         'docid': node.attrib.get('docid'),
  57.         'ordner': node.find('.//ordner').text,
  58.         'hauptordner': node.find('.//hauptordner').text,
  59.         'bemerkung': node.find('.//bemerkung').text,
  60.         'status': node.find('.//status').text,
  61.         'revision': node.find('.//revision').text,
  62.         'dokumentenart': node.find('.//dokumentenart').text,
  63.         'Datum': node.find('.//datum').text,
  64.         'bu-monat': node.find('.//bu-monat').text,
  65.         'bu-jahr': node.find('.//bu-jahr').text,
  66.         'firma': node.find('.//firma').text})
  67.  
  68. file_name = 'export-' + rows[0]['firma'] + '-' + rows[0]['bu-jahr'] + rows[0]['bu-monat']
  69.  
  70. df = pd.DataFrame(rows, columns=cols)
  71. df.index.name = 'No'
  72. df.to_csv(file_name + '.csv')
  73.  
  74. with open(file_name + '.html', 'w') as f:
  75.     f.write(html.format(table=df.to_html(columns=print_cols, index_names=False)))
  76.  
  77. weasyprint.HTML(file_name + '.html').write_pdf(file_name + '.pdf')
RAW Paste Data