浮点数保留位数
from decimal import Decimal
a = Decimal(5.333333333333).quantize(Decimal("0.00"))
print(a)
图片拼接
from PIL import Image
def join(png1, png2, flag='horizontal'):
"""
:param png1: path
:param png2: path
:param flag: horizontal or vertical
:return:
"""
img1, img2 = Image.open(png1), Image.open(png2)
size1, size2 = img1.size, img2.size
if flag == 'horizontal':
joint = Image.new('RGB', (size1[0]+size2[0], size1[1]))
loc1, loc2 = (0, 0), (size1[0], 0)
joint.paste(img1, loc1)
joint.paste(img2, loc2)
joint.save('horizontal.png')
elif flag == 'vertical':
joint = Image.new('RGB', (size1[0], size1[1]+size2[1]))
loc1, loc2 = (0, 0), (0, size1[1])
joint.paste(img1, loc1)
joint.paste(img2, loc2)
joint.save('vertical.png')
if __name__ == '__main__':
png = 'lena.png'
join(png, png)
join(png, png, flag='vertical')
原文链接: https://www.zhangshengrong.com/p/7B1Lq6pPaw/
【导出】功能总结
import openpyxl
from utils.excel_utils import ExcelUtils
from openpyxl.writer.excel import save_virtual_workbook
from django.http import JsonResponse, HttpResponse
def cvt_pool_history_export(ls_obj):
wb = openpyxl.Workbook()
ws = wb.active
ws.append(["调整日期", "简称"])
for dic in ls_obj:
ws.append((
dic.get("created_time"),
dic.get("simple_name"),
))
ExcelUtils.format_excel(ws)
ExcelUtils.simple_fmt_wh(ws)
return save_virtual_workbook(wb)
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
serializer = self.get_serializer(queryset, many=True)
buffer = cvt_pool_history_export(serializer.data)
response = HttpResponse(
buffer, content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
response["Content-Disposition"] = "attachment; filename=XX导出.xlsx"
return response