d100.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. '''Created Jan 25, 2022 Levi'''
  2. from collections import defaultdict
  3. import xlsxwriter as xlsw
  4. from lxml import etree as ET
  5. from declarations.declaration import Declaration
  6. class D100(Declaration):
  7. def __init__(self):
  8. super().__init__()
  9. def from_xml(self, fp):
  10. self.obligs = []
  11. xml = ET.parse(fp)
  12. root = xml.getroot()
  13. def_root = defaultdict(lambda: 0)
  14. def_root.update(root.attrib)
  15. self.def_root = def_root
  16. self.cui = def_root['cui']
  17. self.den = def_root['den']
  18. self.month = int(def_root['luna'])
  19. self.year = int(def_root['an'])
  20. self.root = root
  21. self.decl_type = self.root.tag.split("}", 1)[-1]
  22. # print(root.nsmap)
  23. self.tree = xml.xpath('/x:declaratie100/*', namespaces={'x': 'mfp:anaf:dgti:d100:declaratie:v2'})
  24. for oblig in self.tree:
  25. print(oblig.attrib)
  26. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['cod_oblig']))
  27. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['cota']))
  28. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['suma_dat']))
  29. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['suma_ded']))
  30. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['suma_plata']))
  31. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['suma_rest']))
  32. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['suma_bonif']))
  33. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['suma_spons']))
  34. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['suma_AMEF']))
  35. # print(self.obligs)
  36. return self
  37. @property
  38. def data(self):
  39. data = {
  40. self.cui:
  41. [
  42. self.den,
  43. self.month,
  44. self.year,
  45. ]
  46. }
  47. self.dict_data = data
  48. return self.dict_data
  49. def to_xlsx(self, xl_name, w_sheet_name, files, headers=[], **kwargs):
  50. book = xlsw.Workbook(xl_name + '.xlsm')
  51. book.add_vba_project('./vbaProject.bin')
  52. sheet = book.add_worksheet(w_sheet_name)
  53. # sheet2 = book.add_worksheet('all')
  54. row, row2, col = 1, 1, 0
  55. headformat = book.add_format({'bold': True})
  56. hcol, hcol2 = 0, 0
  57. for h in headers:
  58. sheet.write(1, hcol, h, headformat)
  59. hcol += 1
  60. hcol2 += 1
  61. for file in files:
  62. d = D100() # obj['type']()
  63. d = d.from_xml(file)
  64. for i in d.data:
  65. row += 1
  66. sheet.write(row, col, i)
  67. print(f'write {row}', i)
  68. data = d.data[i]
  69. data.extend(d.obligs)
  70. sheet.write_row(row, 1, data) # d.data[i]
  71. for k, a in kwargs.items():
  72. if k == 'autofilter':
  73. sheet.autofilter(a[0], a[1], 100, 100)
  74. # sheet2.autofilter(a[0], a[1], 1000, 1000)
  75. if k == 'freeze':
  76. sheet.freeze_panes(a[0], a[1])
  77. # sheet2.freeze_panes(a[0], a[1])
  78. book.close()
  79. if __name__ == '__main__':
  80. headers = ['cod fiscal', 'den', 'luna', 'an', 'oblig', 'cota', 'dat', 'ded', 'plata', 'rest', 'bonif', 'spons', 'AMEF',
  81. 'oblig', 'dat', 'ded', 'plata', 'rest', 'bonif', 'spons', 'AMEF']
  82. d100 = D100()
  83. # d.from_xml('../D100_2021_6_17259191_generat.xml')
  84. d100.to_xlsx('D100', 'data', ['../D100_2021_6_17259191_generat.xml'], headers, autofilter=[1, 0], freeze=[2, 1])
  85. # print(d100.data)