Создание excel файла

This commit is contained in:
iriska-gri committed 2023-10-17 12:25:39 +03:00
1 parent d568e27e85
commit 1fa101cf23
3 files changed
+43 -4

No files matched your search

BIN
View File
Binary file not shown.
+43 -4
View File
@@ -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:
Binary file not shown.