d390.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. from xml.etree import ElementTree as ET
  2. import xlsxwriter as xlsw
  3. import openpyxl as xl
  4. from .declaration import Declaration
  5. class D390(Declaration):
  6. def __init__(self,
  7. cui=None,
  8. den=None,
  9. addr=None,
  10. month=None,
  11. year=None,
  12. control=None,
  13. decl_name=None,
  14. decl_func=None,
  15. rect=None,
  16. root=None,
  17. n_operations=None,
  18. bazaL=None,
  19. bazaT=None,
  20. bazaA=None,
  21. bazaP=None,
  22. bazaS=None,
  23. bazaR=None,
  24. total_baza=None,
  25. decl_type=None):
  26. super().__init__(cui=cui,
  27. den=den,
  28. addr=addr,
  29. month=month,
  30. year=year,
  31. control=control,
  32. decl_name=decl_name,
  33. decl_func=decl_func,
  34. rect=rect,
  35. root=root,
  36. decl_type=decl_type)
  37. self.n_operations = n_operations,
  38. self.bazaL = bazaL,
  39. self.bazaT = bazaT,
  40. self.bazaA = bazaA,
  41. self.bazaP = bazaP,
  42. self.bazaS = bazaS,
  43. self.bazaR = bazaR,
  44. self.total_baza = total_baza
  45. def from_xml(self, fp):
  46. xml = ET.parse(fp)
  47. root = xml.getroot()
  48. # self.root = root
  49. decl_type = root.tag.split("}", 1)[-1]
  50. return D390(cui=root.attrib['cui'],
  51. den=root.attrib['den'],
  52. addr=root.attrib['adresa'],
  53. month=int(root.attrib['luna']),
  54. year=int(root.attrib['an']),
  55. rect=int(root.attrib['d_rec']),
  56. control=int(root.attrib['totalPlata_A']),
  57. n_operations=int(root[0].attrib['nrOPI']),
  58. bazaL=int(root[0].attrib['bazaL']),
  59. bazaT=int(root[0].attrib['bazaT']),
  60. bazaA=int(root[0].attrib['bazaA']),
  61. bazaP=int(root[0].attrib['bazaP']),
  62. bazaS=int(root[0].attrib['bazaS']),
  63. bazaR=int(root[0].attrib['bazaR']),
  64. total_baza=int(root[0].attrib['total_baza']),
  65. root=root,
  66. decl_type=decl_type)
  67. def from_xml_all(self, fp):
  68. xml = ET.parse(fp)
  69. root = xml.getroot()
  70. op_list = []
  71. for ch in root:
  72. if 'tip' in ch.attrib:
  73. try:
  74. d = {
  75. root.attrib['cui']: [
  76. int(root.attrib['luna']),
  77. int(root.attrib['an']),
  78. ch.attrib['tip'],
  79. ch.attrib['tara'],
  80. ch.attrib['codO'],
  81. ch.attrib['denO'],
  82. ch.attrib['baza']
  83. ]
  84. }
  85. op_list.append(d)
  86. except KeyError:
  87. pass
  88. self.op_list = op_list
  89. return self.op_list
  90. @property
  91. def data(self):
  92. data = {
  93. self.cui:
  94. [
  95. self.den[0], self.addr[0], self.month[0], self.year[0],
  96. self.rect, self.control[0], self.n_operations[0],
  97. self.bazaT[0], self.bazaL[0], self.bazaA[0],
  98. self.bazaP[0], self.bazaS[0], self.bazaR[0], self.total_baza
  99. ]
  100. }
  101. self.dict_data = data
  102. return self.dict_data
  103. @property
  104. def headers(self):
  105. header = [h for h in self.root.attrib.keys()]
  106. ops = [h for h in self.root[0].attrib.keys()]
  107. del(ops[0])
  108. header.extend(ops)
  109. self.header = header
  110. return self.header
  111. def to_xlsx(self, xl_name, w_sheet_name, files, headers=[], obj={'type': None}, **kwargs):
  112. book = xlsw.Workbook(xl_name + '.xlsm')
  113. book.add_vba_project('./vbaProject.bin')
  114. sheet = book.add_worksheet(w_sheet_name)
  115. sheet2 = book.add_worksheet('all')
  116. row, row2, col = 1, 1, 0
  117. headformat = book.add_format({'bold': True})
  118. hcol, hcol2 = 0, 0
  119. for h in headers:
  120. sheet.write(1, hcol, h, headformat)
  121. hcol += 1
  122. for _ in range(8):
  123. sheet2.write(1, hcol2, 'x', headformat)
  124. hcol2 += 1
  125. for file in files:
  126. d = D390() # obj['type']()
  127. d = d.from_xml(file)
  128. for i in d.data:
  129. row += 1
  130. sheet.write(row, col, i)
  131. print(f'write {row}', i)
  132. sheet.write_row(row, 1, d.data[i])
  133. d_all = d.from_xml_all(file)
  134. for el in d_all:
  135. for dict_ in el:
  136. row2 += 1
  137. sheet2.write(row2, col, dict_)
  138. sheet2.write_row(row2, 1, el[dict_])
  139. for k, a in kwargs.items():
  140. if k == 'autofilter':
  141. sheet.autofilter(a[0], a[1], 100, 100)
  142. sheet2.autofilter(a[0], a[1], 1000, 1000)
  143. if k == 'freeze':
  144. sheet.freeze_panes(a[0], a[1])
  145. sheet2.freeze_panes(a[0], a[1])
  146. book.close()
  147. # self._format_sh1(xl_name)
  148. # self._format_sh2(xl_name)
  149. def _format_sh1(self, xl_name):
  150. wb = xl.load_workbook(xl_name + '.xlsx')
  151. sh = wb.worksheets[0]
  152. colG = sh['G3':'G{}'.format(sh.max_row)]
  153. colIO = sh['I3':'O{}'.format(sh.max_row)]
  154. for valuesG, valuesIO in zip(colG, colIO):
  155. for value in valuesG:
  156. if isinstance(value.value, str):
  157. try:
  158. num_value = float(value.value)
  159. value.value = num_value
  160. except ValueError as ve:
  161. print(str(ve))
  162. value.number_format = '#,##0.00'
  163. for value in valuesIO:
  164. if isinstance(value.value, str):
  165. try:
  166. num_value = float(value.value)
  167. value.value = num_value
  168. except ValueError as ve:
  169. print(str(ve))
  170. value.number_format = '#,##0.00'
  171. wb.save(xl_name + '.xlsx')
  172. wb.close()
  173. def _format_sh2(self, xl_name):
  174. wb = xl.load_workbook(xl_name + '.xlsx')
  175. sh = wb.worksheets[1]
  176. colH = sh['H3':'H{}'.format(sh.max_row)]
  177. for valuesG in colH:
  178. for value in valuesG:
  179. if isinstance(value.value, str):
  180. try:
  181. num_value = float(value.value)
  182. value.value = num_value
  183. except ValueError as ve:
  184. print(str(ve))
  185. value.number_format = '#,##0.00'
  186. wb.save(xl_name + '.xlsx')
  187. wb.close()