gcstruct.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import pandas as pd
  2. import xml.etree.ElementTree as ET
  3. import json
  4. import re
  5. from bs4 import BeautifulSoup
  6. config = ["SKR51", "Herkunft_KST", "Absatzkanal", "Kostentraeger", "Marke", "Standort", "Manuelle_Konten"]
  7. columns = ["Konto_Nr", "Konto_Bezeichnung", "Konto_Art", "Kostenstelle", "STK", "Konto_1", "Konto_2", "Konto_3", "Konto_4", "Konto_5"]
  8. def get_tree(node, parents):
  9. result = []
  10. for child in node:
  11. parents.append(child.attrib['Name'])
  12. result.append({ "id": ";".join(parents) + ";"*(10 - len(parents)), "text": child.attrib['Name'], "children": get_tree(child, parents) })
  13. parents.pop()
  14. return result
  15. def structure_from_tree(node):
  16. result = []
  17. result.append(node['id'])
  18. for child in node['children']:
  19. result.extend(structure_from_tree(child))
  20. return result
  21. def xml_from_tree (xml_node, tree_node):
  22. for child in tree_node['children']:
  23. element = ET.SubElement(xml_node, "Ebene")
  24. element.set("Name", child['text'])
  25. xml_from_tree(element, child)
  26. def split_it(text, index):
  27. try:
  28. return re.findall(r"([^;]+) - ([^;]*);;", text)[0][index]
  29. except:
  30. return ""
  31. def get_structure_and_tree ():
  32. df = pd.read_csv("gcstruct/Kontenrahmen/Kontenrahmen.csv", sep=";", encoding="ansi", decimal=",", converters={i: str for i in range(0, 200)}) #, index_col="Konto_Nr")
  33. for i, structure in enumerate(config):
  34. first = i*10 + 1
  35. df[structure] = df['Ebene'+str(first)] + ";" + df['Ebene'+str(first+1)] + ";" + df['Ebene'+str(first+2)] + ";" + df['Ebene'+str(first+3)] + ";" + df['Ebene'+str(first+4)] + ";" + df['Ebene'+str(first+5)] + ";" + df['Ebene'+str(first+6)] + ";" + df['Ebene'+str(first+7)] + ";" + df['Ebene'+str(first+8)] + ";" + df['Ebene'+str(first+9)]
  36. df['LetzteEbene' + str(i+1) + '_Nr'] = df[structure].apply(lambda x: split_it(x, 0))
  37. df['LetzteEbene' + str(i+1) + '_Bez'] = df[structure].apply(lambda x: split_it(x, 1))
  38. df = df[columns + config + ['LetzteEbene' + str(i+1) + '_Nr' for i in range(len(config))] + ['LetzteEbene' + str(i+1) + '_Bez' for i in range(len(config))]]
  39. json_result = { "Kontenrahmen": df.to_dict("records") }
  40. # df2 = pd.read_csv("gcstruct/Strukturen/Kontenrahmen.csv/SKR51.csv", sep=";", encoding="ansi", decimal=",", converters={i: str for i in range(0, 200)})
  41. # print(df2.head())
  42. for i, structure in enumerate(config):
  43. tree = ET.parse("gcstruct/Xml/" + structure + ".xml")
  44. json_result[structure] = get_tree(tree.getroot(), [])
  45. json.dump(json_result, open("gcstruct/SKR51.json", "w"), indent=2)
  46. def post_structure_and_tree ():
  47. json_post = json.load(open("gcstruct/SKR51.json", "r"))
  48. # Kontenrahmen.csv
  49. ebenen = ["Ebene" + str(i) for i in range(1, len(config)*10+1)]
  50. header = ";".join(columns + ebenen)
  51. cols = columns + config
  52. with open("gcstruct/Kontenrahmen/Kontenrahmen_out.csv", "w", encoding="ansi") as f:
  53. f.write(header + "\n")
  54. for row in json_post['Kontenrahmen']:
  55. f.write(";".join([row[e] for e in cols]) + "\n")
  56. # print(header)
  57. # xml und evtl. Struktur.csv
  58. for i, structure in enumerate(config):
  59. with open("gcstruct/Strukturen/Kontenrahmen.csv/" + structure + "_out.csv", "w", encoding="ansi") as f:
  60. f.write(";".join(["Ebene" + str(i*10 + j) for j in range(1, 11)]) + "\n")
  61. rows = structure_from_tree({"id": ";"*9, "children": json_post[structure] })
  62. f.write("\n".join(rows))
  63. #with open("gcstruct/Strukturen/Kontenrahmen.csv/" + structure + "_2.csv", "w", encoding="ansi") as f:
  64. root = ET.Element("Ebene")
  65. root.set("Name", structure)
  66. xml_from_tree(root, {"id": ";"*9, "children": json_post[structure] })
  67. with open("gcstruct/Xml/" + structure + "_out.xml", "w", encoding="utf-8") as f:
  68. f.write(BeautifulSoup(ET.tostring(root), "xml").prettify())
  69. get_structure_and_tree()
  70. # post_structure_and_tree()