info_anaf.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # coding: utf-8
  2. import os
  3. import platform
  4. import time
  5. import subprocess as sp
  6. from json import JSONDecodeError
  7. import requests
  8. # import ujson
  9. # import openpyxl as xl
  10. # from openpyxl.utils import get_column_letter
  11. import xlsxwriter as xlsw
  12. def file_open(file_name):
  13. abs_path = os.path.abspath(file_name)
  14. if platform.system() == 'Windows':
  15. sp.run(f'start {abs_path}', shell=True)
  16. elif platform.system() == 'Linux':
  17. sp.run(['xdg-open', f'{abs_path}'])
  18. else:
  19. raise NotImplementedError
  20. def dict_to_list(s_dict, from_ind=None):
  21. if from_ind is None: n_list = [s_dict[i] for i in s_dict]
  22. else:
  23. n_list = [s_dict[i] for i in s_dict]
  24. n_list.pop(from_ind)
  25. return n_list
  26. def insert_keys(dict_, obj, pos):
  27. return {k: v for k, v in (list(dict_.items())[:pos] + list(obj.items()) + list(dict_.items())[pos:])}
  28. def check_list(xl_name, file_name=None, list_=None, excl_=[], _service_url='https://webservicesp.anaf.ro/PlatitorTvaRest/api/v6/ws/tva'):
  29. if file_name:
  30. with open(file_name, 'r') as fiscal_codes, open("lista_cf_new.txt", "w") as fileout:
  31. for code in fiscal_codes:
  32. if code[0:2] != 'HU' and code[0:2] != 'DE' and code[0:2] != 'GB' and code[0:2] != 'EL' and code[0:2] != 'LT' and 'SK' and code not in excl_:
  33. fileout.write(code)
  34. with open("lista_cf_new.txt", "r") as newfile:
  35. lista_cf_ro = ' '.join(newfile).replace(',', '').split()
  36. if list_:
  37. lista_cf_ro = list_
  38. dest_book = xlsw.Workbook(xl_name + '.xlsm')
  39. dest_book.add_vba_project('./vbaProject.bin')
  40. w_sheet = dest_book.add_worksheet('anaf')
  41. my_row, my_col = 0, 0
  42. # headers not needed in this version
  43. # insert = lambda dict_, obj, pos: {k: v for k, v in (list(dict_.items())[:pos] + list(obj.items()) + list(dict_.items())[pos:])}
  44. print('items in list', len(lista_cf_ro))
  45. for code in lista_cf_ro:
  46. payload = [{"cui": code.replace('RO', ''), "data": time.strftime('%Y-%m-%d')}]
  47. response = requests.post(_service_url, json=payload, headers={'Content-Type': 'application/json'})
  48. try:
  49. resp_dict = response.json()['found'][0]
  50. random_keys = ['nrRegCom', 'telefon', 'fax', 'codPostal', 'act', 'stare_inregistrare']
  51. pos = 4
  52. for random_key in random_keys:
  53. if random_key not in resp_dict.keys():
  54. # resp_dict[random_key] = 'NaN'
  55. resp_dict = insert_keys(resp_dict, {random_key: 'Nan'}, pos)
  56. pos += 1
  57. # print(resp_dict)
  58. new_dict = {resp_dict['cui']: dict_to_list(resp_dict, 0)}
  59. for i in new_dict:
  60. my_row += 1
  61. print(i)
  62. w_sheet.write(my_row, 0, code)
  63. w_sheet.write(my_row, 1, i)
  64. w_sheet.write_row(my_row, 2, new_dict[i])
  65. except (KeyError, JSONDecodeError):
  66. pass
  67. w_sheet.write(0, my_col, 'code mentor')
  68. for keys in resp_dict.keys():
  69. w_sheet.write(0, my_col + 1, keys)
  70. my_col += 1
  71. w_sheet.set_column(3, 3, 50)
  72. w_sheet.autofilter(0, 0, len(lista_cf_ro), len(resp_dict.keys()))
  73. w_sheet.freeze_panes(1, 0)
  74. # w_sheet.set_column(0, 1, 25)
  75. dest_book.close()
  76. print('{0}\\data.xlsx'.format(os.getcwd()))
  77. # autosize all the columns in wsheet
  78. # dims = {}
  79. # data = xl.load_workbook('data.xlsx')
  80. # w_sheet = data.worksheets[0]
  81. # for row in w_sheet.rows:
  82. # for cell in row:
  83. # if cell.value:
  84. # dims[cell.column_letter] = max((dims.get(cell.column_letter, 0), len(str(cell.value))))
  85. # for col, value in dims.items():
  86. # w_sheet.column_dimensions[col].width = value + 1
  87. # data.save('data.xlsx')
  88. # file_open('data')
  89. # os.startfile('{0}/data.xlsx'.format(os.getcwd())) # or subprocess.check_call('start {0}/data.xlsx'.format(os.getcwd()), shell=True)
  90. # with win32 : xl = Dispatch("Excel.Application")
  91. # xl.Visible = True
  92. # xl.Workbooks.Open(Filename=os.getcwd() + '\\data.xlsx')