xls_cond_form.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. '''Created 26 May 2022 Levi'''
  2. import calendar
  3. from datetime import date
  4. from datetime import timedelta
  5. import openpyxl as xl
  6. import yagmail
  7. from openpyxl import styles
  8. from openpyxl import formatting
  9. from openpyxl.styles.borders import Side
  10. class MySMTP(yagmail.SMTP):
  11. def __enter__(self):
  12. pass
  13. def __exit__(self, exc_type, exc_val, exc_tb):
  14. pass
  15. path = 'C:/Users/Levi/Downloads/ciel/dlg_25.07.2022.xlsx'
  16. if date.today().day > 15:
  17. start = date.today() - timedelta(days=date.today().day - 1)
  18. end = date(date.today().year, date.today().month, 15)
  19. else:
  20. if date.today().month == 1:
  21. start = date(date.today().year - 1, 12, 15)
  22. end = date(date.today().year - 1, 12, calendar.monthrange(date.today().year - 1, 12)[1])
  23. else:
  24. start = date(date.today().year, date.today().month - 1, 15)
  25. end = date(date.today().year, date.today().month - 1, calendar.monthrange(date.today().year, date.today().month - 1)[1])
  26. subject = 'Delegacio elszamolas {} -> {}'.format(start, end)
  27. body = '''<p>Delegacio elszamolas <span style="color:Tomato"><b>{} -> {}</b>.</span></p>
  28. <p>A csatolt tablazatban le lehet ellenorizni a fenti periodusra:</p>
  29. <li>delegaciokat (C oszlop delegacio szam, D oszlop periodus, H oszlop napi dij),</li>
  30. <li>egyeb elszamolni valot mint szallas szamlak, egyebek (leroy stb...)</li>
  31. '''.format(start, end)
  32. to = ['office@mzk.ro']
  33. # to = ['deeejas@gmail.com']
  34. yag = MySMTP(user={'levente.marton@mzk.ro': 'levente.marton@mzk.ro'}, host='192.168.201.4', port=25, smtp_starttls=True, smtp_ssl=False)
  35. attachments = [path]
  36. bottom_border = styles.Border(bottom=Side(style='thin'))
  37. wb = xl.load_workbook(path)
  38. ws = wb.worksheets[0]
  39. for i in range(1, ws.max_row + 1):
  40. if ws.cell(row=i, column=3).value in ['SOLD INITIAL', 'TOTAL', 'TOTAL ANGAJAT']:
  41. ws.delete_rows(i, 1)
  42. # print(f'i = {i}\tcell value (i, 1) is {ws.cell(row=i, column=1).value}')
  43. for i in range(1, ws.max_row + 1):
  44. if ws.cell(row=i, column=1).value in ['Cont: TOTAL ANGAJAT ']:
  45. ws.delete_rows(i, 1)
  46. # print(f'i = {i}\tcell value (i, 1) is {ws.cell(row=i, column=1).value}')
  47. ws.delete_cols(10)
  48. col_dims = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'}
  49. dims = {}
  50. for row in ws.rows:
  51. for cell in row:
  52. if cell.value:
  53. dims[cell.column_letter] = max((dims.get(cell.column_letter, 0), len(str(cell.value))))
  54. for col, value in dims.items():
  55. if col in col_dims:
  56. ws.column_dimensions[col].width = value + 2
  57. ws.conditional_formatting.add(
  58. 'A3:I100',
  59. formatting.rule.FormulaRule(formula=['NOT($C4=$C3)'], stopIfTrue=True, border=bottom_border)
  60. )
  61. wb.save(path)
  62. wb.close()
  63. print(subject, end='\n')
  64. print(body)
  65. yag.send(to=to, subject=subject, contents=body, attachments=attachments)
  66. yag.close()