|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
下载图片的方法- def download_to_excel_pic(datas: list, fields: list, pic_col: int,
- pic_dir='images',
- filename='eg', *args, **kwargs):
-
- pic_path = os.path.join(settings.STATIC_ROOT, pic_dir)
- pic_col_name = string.ascii_uppercase[pic_col-1]
- wb = Workbook()
- ws = wb.active
- ws.append(fields)
- for idx, item in enumerate(datas):
- row_list = [row for row in item.values()]
- row_list.insert(pic_col - 1, '')
- ws.append(row_list)
- ws.row_dimensions[idx + 2].height = 40
-
- pic_name = str(row_list[pic_col - 2]) + '.jpg'
- img_path = os.path.join(pic_path, pic_name)
- try:
- img = Image(img_path)
- img.width = 40
- img.height = 40
- ws.add_image(img, f'{pic_col_name}{idx + 2}')
- except Exception as e:
- ws[f'{pic_col_name}{idx + 2}'] = '图片不存在'
-
- excel_file = BytesIO()
- wb.save(excel_file)
- excel_file.seek(0)
- response = HttpResponse(content=excel_file.getvalue(),
- content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
- response['Content-Disposition'] = f'attachment; filename="{filename}.xlsx"'
- return response
复制代码
上面代码是 一个 django 下载图片到excel的 方法;
在开发环境时, 能正常下载图片, ;
在正式环境后, 却下载不了图片, 图片能正常在页面显示 的。
img_path 是图片的绝对路径 ,检查过 绝对 路径是正确的。
为什么会出现这种情况,
或者还有什么方法 可以把图片 下载到excel 中 ??? |
|