From ebbedd6aae0e3f70f75febd2051c0351948aec59 Mon Sep 17 00:00:00 2001 From: iriska-gri <79623434+iriska-gri@users.noreply.github.com> Date: Tue, 17 Oct 2023 12:25:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20excel=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db.sqlite3 | Bin 376832 -> 376832 bytes general_vacation/views.py | 47 ++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/db.sqlite3 b/db.sqlite3 index 88238d80d0f683b8984b19a681dd1b5c4ae5bd34..840da3e9bc250ceb433d4d86b25855fb76f42692 100644 GIT binary patch delta 386 zcmZo@5N~J@pCHYsJyFJ)QF~*8tUhBwbDe&Boj&9CI(?>KeQk3E15+zg3o8Q?J!4BF zLt|qj10!QyLjzqy44LhJ;+QxbWxSc^Gq4_K;b)!7TF;uv`kalKRg9I5C5HJr^Ze~H z3z+Jer^hT|GG{D+Q2E>WmoR;mX7=U_oX&oLNtFpm>l|Q8Vd4whJ^{$*+P?D?(+p;A z7G?&<0#;^5M*jTvm~%`(%)CA39LtJ>0&IM>4E($Ji}@}1Ir$Fp)ovCv$mHX2E?{FQ z=44Qup0I?;SP0>2*L6HX5wYCVyb6~ zVfw+W$asKpG4mCMODq!8WfriSZ4X<(TEr~KugG{|{)K%P8!k3oaAqwA+84is$wUBQ hYJm%DG0--U==6YxOk7On`O`l>Wis17c?oN-Gys}PYf1nB delta 278 zcmZo@5N~J@pCHYsI#I@%QFUX2tUhC2bDe&Boj&9CI(?>KeQh%Z15+ylLn~85Jp(gK zb7M;*10!QyLjzqy44LhJ;+QxbB|KU98CZ|A@Uu>3t!K?-ea^3K#zq{(Jmq`QGxK-fXBK&$WH$DW)0B)8kJw zv9VexvN6QA$DCsVV&?5J=U7%8V$U(9d8&$(I9;3prGZr*giOH7K>b>_3) z;8A6m$-s1)iI>TWseU@&114u?ZSIfT!xpd>F*DAX-uRTsWP0B&R)gsRyI9$#>+J** PmQR>sw@+Kbnk5YYnr2bd diff --git a/general_vacation/views.py b/general_vacation/views.py index ae3a538fa..e64863af2 100644 --- a/general_vacation/views.py +++ b/general_vacation/views.py @@ -1,7 +1,8 @@ import os from openpyxl import load_workbook +import openpyxl from rest_framework.response import Response -import datetime +from datetime import date from .serializers import * from .models import * import pandas as pd @@ -11,6 +12,9 @@ from django.shortcuts import render from pandas.io.excel import ExcelWriter import openpyxl as oxl from openpyxl import Workbook +from openpyxl.styles import Font, Color +from openpyxl.styles import Alignment + class GeneralVacationAPIView(APIView): def get(self, request): @@ -30,20 +34,55 @@ class GeneralVacationAPIView(APIView): return Response({"Отпуск": serializer_for_basecrit.data, "План-график": serializer_for_plan.data}) class DailyRoundUpAPIView(APIView): + + def convert_width(self, width): + """ + Конвертируем ширину столбца Excel в ширину столбца openpyxl + """ + PIXELS_PER_CHAR = 7 + return (width) / PIXELS_PER_CHAR + def get(self, request): user = DictEmployee.objects.using('user_directory').values('login', 'department', 'familyname', 'name', 'parentname') df = pd.DataFrame.from_records(user) df['tabel'] = df['login'].str.split('-', expand = True)[1]+df['login'].str.split('-', expand = True)[2] file_path = 'media/DailyRoundUp.xlsx' + wb = Workbook() ws = wb.active ws.title = "New Title" - ws3 = wb["New Title"] - ws['A4'] = 1111 + ws['B2'] = 'Список сотрудников' + ws['B3'] = 'МИ ФНС России по ЦОД № 2' + ws['B4'] = 'по подразделениям' + + + ws['I1'] = 'На '+ date.today().strftime("%d.%m.%Y") + + + + column_widths = [29, 53, 96, 96, 112, 28, 222, 29, 148, 28, 145, 33] + for i, width in enumerate(column_widths): + column_letter = openpyxl.utils.get_column_letter(i+1) + ws.column_dimensions[column_letter].width = self.convert_width(width) + + for col in ws.iter_cols(min_row=2, max_row=5, min_col=2, max_col=5): + for cell in col: + cell.font = Font(name='Times New Roman', size=12, bold=True) + cell.alignment = Alignment(horizontal='center') + + ws['I1'].font = Font(name='Times New Roman', size=14, bold=True) + ws['I1'].alignment = Alignment(horizontal='left') + + + for x in range(3): + ws.merge_cells(start_row=2+x, start_column=2, end_row=2+x, end_column=6) + + wb.save(file_path) - print(ws3, '----------------------') + # file_path = 'media/DailyRoundUp.xlsx' + return Response('') # with ExcelWriter(file_path, mode="a" if os.path.exists(file_path) else "w" , if_sheet_exists='replace') as writer: