parser_intrastat.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from time import (time, gmtime, strftime)
  2. import openpyxl as xl
  3. from openpyxl.styles import (Font, NamedStyle)
  4. def time_it(func):
  5. def wrapper(*args, **kwargs):
  6. start = time()
  7. result = func(*args, **kwargs)
  8. end = time()
  9. mlsec = repr(end - start).split('.')[1][:3]
  10. elapsed = strftime(f"%M:%S.{mlsec}", gmtime(end - start)) # noqa E:231
  11. print(f'{func.__name__} finished in {elapsed}')
  12. return result
  13. return wrapper
  14. def header_create(sheet, headers=[]):
  15. if len(headers) == 0:
  16. headers = ['DATA', 'FURNIZOR', 'CF', 'CountryOfOrigin', 'EXP', 'DeliveryTermsCode',
  17. 'ACode', 'Bcode', 'TransportCode', 'INVNR', 'VSCODE', 'NAME', 'GROUP',
  18. 'UM', 'Q', 'PRICE', 'NETWEIGHT', 'NETVALUE', 'SUPPL', 'SUPPLVALUE',
  19. 'Ppacking', 'Spacking', 'Paper', 'Plastic', 'Wooden']
  20. for n, h in enumerate(sheet.iter_cols(min_row=1, max_col=len(headers), max_row=1)):
  21. h[0].value = headers[n]
  22. for f in sheet['A1:Y1'][0]:
  23. f.font = Font(bold=True)
  24. def format_after(sheet, ref=None):
  25. date_style = NamedStyle(name='datetime', number_format='DD/MMM/YYYY')
  26. if not ref:
  27. ref = f'A1:Y{sheet.max_row}' # noqa E:231
  28. sheet.freeze_panes = 'A2'
  29. sheet.auto_filter.ref = ref
  30. for cell in sheet.iter_rows(min_row=2, max_col=1, max_row=sheet.max_row):
  31. cell[0].style = date_style
  32. @time_it
  33. def parse_intrastat(articles, export, partners):
  34. ib = xl.load_workbook(articles)
  35. isheet = ib.worksheets[0]
  36. cb = xl.load_workbook(export)
  37. csheet = cb.worksheets[0]
  38. pb = xl.load_workbook(partners)
  39. psheet = pb.worksheets[0]
  40. xml_book = xl.Workbook()
  41. xml_book.create_sheet('intrastat', 0)
  42. xml_sheet = xml_book.worksheets[0]
  43. header_create(xml_sheet)
  44. for n, p in enumerate(csheet.iter_rows(min_row=2, max_col=14, max_row=csheet.max_row)): # .max_row - 1
  45. xml_sheet[f'A{n+2}'] = p[4].value # noqa E:226
  46. xml_sheet[f'B{n+2}'] = p[0].value # noqa E:226
  47. xml_sheet[f'L{n+2}'] = p[9].value # noqa E:226
  48. xml_sheet[f'N{n+2}'] = p[11].value # noqa E:226
  49. xml_sheet[f'O{n+2}'] = p[10].value # noqa E:226
  50. xml_sheet[f'P{n+2}'] = p[12].value # noqa E:226
  51. xml_sheet[f'R{n+2}'] = p[13].value # noqa E:226
  52. for c in psheet.iter_rows(min_row=2, max_col=9, max_row=psheet.max_row):
  53. if xml_sheet[f'B{n+2}'].value == c[1].value: # noqa E:226
  54. xml_sheet[f'C{n+2}'] = c[2].value # noqa E:226
  55. xml_sheet[f'D{n+2}'] = c[3].value # noqa E:226
  56. xml_sheet[f'E{n+2}'] = c[4].value # noqa E:226
  57. xml_sheet[f'F{n+2}'] = c[5].value # noqa E:226
  58. xml_sheet[f'G{n+2}'] = c[6].value # noqa E:226
  59. xml_sheet[f'H{n+2}'] = c[7].value # noqa E:226
  60. xml_sheet[f'I{n+2}'] = c[8].value # noqa E:226
  61. for i in isheet.iter_rows(min_row=2, max_col=10, max_row=isheet.max_row):
  62. if xml_sheet[f'L{n+2}'].value == i[1].value: # noqa E:226
  63. xml_sheet[f'K{n+2}'] = i[3].value # noqa E:226
  64. xml_sheet[f'Q{n+2}'] = i[9].value * xml_sheet[f'O{n+2}'].value # noqa E:226
  65. format_after(xml_sheet)
  66. xml_book.save('intrastat.xlsx')
  67. __all__ = ['xl', 'header_create', 'format_after', 'parse_intrastat']
  68. if __name__ == '__main__':
  69. parse_intrastat('intrastat_art.xlsx', 'Export.xlsx', 'intrastat_part.xlsx')