dec_100_old.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import os
  2. # from xml.etree import ElementTree as et
  3. import PyPDF2
  4. import xlsxwriter as xlsw
  5. import openpyxl as xl
  6. def filter_files(_ext, path=None):
  7. '''lists all pdf's in current directory'''
  8. if path:
  9. my_files = filter(lambda file: file.endswith(_ext) or
  10. file.endswith(_ext.upper()), os.listdir(path))
  11. else:
  12. my_files = filter(lambda file: file.endswith(_ext) or
  13. file.endswith(_ext.upper()), os.listdir())
  14. return my_files
  15. def add_dict_item(my_dict, **kwargs):
  16. for k in kwargs:
  17. t_dict = {k: kwargs[k]}
  18. my_dict.update(t_dict)
  19. return my_dict
  20. def dict_to_list(s_dict, from_ind=None):
  21. if from_ind is None:
  22. n_list = [s_dict[i] for i in s_dict]
  23. else:
  24. n_list = [s_dict[i] for i in s_dict]
  25. n_list.pop(from_ind)
  26. return n_list
  27. class Type_Error(Exception):
  28. d_types = ['100', '710']
  29. def __init__(self, m='Error, the given declaration type is not valid, valid code`s are:'):
  30. self.type_list = [i for i in Type_Error.d_types]
  31. self.mess = m + str(self.type_list)
  32. def __str__(self):
  33. return self.mess
  34. class Declaratii(object):
  35. def __init__(self, d_type='', _ext='', xl_dict={}):
  36. self.d_type = d_type
  37. self._ext = _ext
  38. self.xl_dict = xl_dict
  39. if d_type not in Type_Error.d_types:
  40. raise Type_Error()
  41. @property
  42. def code_list(self):
  43. return Type_Error.d_types
  44. def add_code(self, _code):
  45. Type_Error.d_types.append(_code)
  46. @property
  47. def get_d_type(self):
  48. return self.d_type
  49. def init_headers(self):
  50. self.headers = []
  51. if self.d_type not in Type_Error.d_types:
  52. raise Type_Error()
  53. if self.d_type == Type_Error.d_types[0]:
  54. self.headers = ['cod_fiscal', 'denumire', 'luna', 'an', 'cod_impozit_1', 'suma_1', 'cod_impozit2', 'suma2']
  55. elif self.d_type == Type_Error.d_types[1]:
  56. self.headers = ['cod_fiscal', 'denumire', 'luna', 'an', 'cod_impozit_1', 'suma_i', 'suma_c', 'cod_impozit2', 'suma_i2', 'suma_c2']
  57. return self.headers
  58. def init_dict(self, _inp):
  59. self.d_dict = {}
  60. if self.d_type not in Type_Error.d_types:
  61. raise Type_Error()
  62. if self.d_type == Type_Error.d_types[0]:
  63. year = 'an_r'
  64. month = 'luna_r'
  65. f_code = 'cif'
  66. c_name = 'denumire'
  67. cod_0 = 'cod_oblig_0'
  68. dat_0 = 'suma_plata_0'
  69. cod_1 = 'cod_oblig_1'
  70. dat_1 = 'suma_plata_1'
  71. cont = PyPDF2.PdfFileReader(_inp).getFormTextFields()
  72. if cod_1 in cont:
  73. self.d_dict = {cont[f_code]: [cont[c_name], cont[month], cont[year], cont[cod_0], cont[dat_0], cont[cod_1], cont[dat_1]]}
  74. else:
  75. self.d_dict = {cont[f_code]: [cont[c_name], cont[month], cont[year], cont[cod_0], cont[dat_0]]}
  76. return self.d_dict
  77. elif self.d_type == Type_Error.d_types[1]:
  78. year = 'an_r'
  79. month = 'luna_r'
  80. f_code = 'cif'
  81. c_name = 'denumire'
  82. cod_0 = 'cod_oblig_0'
  83. dat_i0 = 'suma_plata_I0'
  84. dat_c0 = 'suma_plata_C0'
  85. cod_1 = 'cod_oblig_1'
  86. dat_i1 = 'suma_plata_I1'
  87. dat_c1 = 'suma_plata_C1'
  88. cont = PyPDF2.PdfFileReader(_inp).getFormTextFields()
  89. if cod_1 in cont:
  90. self.d_dict = {cont[f_code]: [cont[c_name], cont[month], cont[year], cont[cod_0], cont[dat_i0], cont[dat_c0],
  91. cont[cod_1], cont[dat_i1], cont[dat_c1]]}
  92. else:
  93. self.d_dict = {cont[f_code]: [cont[c_name], cont[month], cont[year], cont[cod_0], cont[dat_i0], cont[dat_c0]]}
  94. return self.d_dict
  95. def gen_xls(self, xl_name, w_sheet_name, seq, headers=[], **kwargs):
  96. _out = xlsw.Workbook(xl_name + '.xlsx')
  97. _sheet = _out.add_worksheet(w_sheet_name)
  98. _row, _col = 1, 0
  99. headformat = _out.add_format({'bold': True})
  100. hcol = 0
  101. for h in headers:
  102. _sheet.write(1, hcol, h, headformat)
  103. hcol += 1
  104. for s in seq:
  105. for i in self.init_dict(s):
  106. _row += 1
  107. _sheet.write(_row, _col, i)
  108. print(f'write {_row}', i)
  109. _sheet.write_row(_row, 1, self.init_dict(s)[i])
  110. for k, a in kwargs.items():
  111. if k == 'autofilter':
  112. _sheet.autofilter(a[0], a[1], 100, 100)
  113. if k == 'freeze':
  114. _sheet.freeze_panes(a[0], a[1])
  115. _out.close()
  116. wb = xl.load_workbook(xl_name + '.xlsx')
  117. sh = wb.worksheets[0]
  118. colC = sh['C3':'D{}'.format(sh.max_row)]
  119. colF = sh['F3':'F{}'.format(sh.max_row)]
  120. colI = sh['H3':'H{}'.format(sh.max_row)]
  121. for valuesC, valuesF, valuesI in zip(colC, colF, colI):
  122. for value in valuesC:
  123. if isinstance(value.value, str):
  124. try:
  125. num_value = float(value.value)
  126. value.value = num_value
  127. except ValueError as ve:
  128. print(str(ve))
  129. for value in valuesF:
  130. if isinstance(value.value, str):
  131. try:
  132. num_value = float(value.value)
  133. value.value = num_value
  134. except ValueError as ve:
  135. print(str(ve))
  136. value.number_format = '#,##0.00'
  137. for value in valuesI:
  138. if isinstance(value.value, str):
  139. try:
  140. num_value = float(value.value)
  141. value.value = num_value
  142. except ValueError as ve:
  143. print(str(ve))
  144. value.number_format = '#,##0.00'
  145. # value.number_format = '#,##0'
  146. wb.save(xl_name + '.xlsx')
  147. wb.close()
  148. _decl = Declaratii('100', '.pdf')
  149. # for i in recipisa.filter_files('.xml'):
  150. # print(_decl.init_dict(i))
  151. _decl.gen_xls('D100', 'data', filter_files('.pdf'), _decl.init_headers(),
  152. autofilter=[1, 0], freeze=[2, 1])