我有django站点,在那里我试图创建一个包含图像的excel文件。
我正在使用: xlsxwriter==1.4.5,并试图用:
worksheet_s.insert_image(5, thisColumn, str('https://unord-tools-django-project-static.s3.eu-central-1.amazonaws.com/media/public/'+image))我的模型看起来是这样的:
class Room(models.Model):
# Relationships
location = models.ForeignKey("asset_app.Locations", on_delete=models.SET_NULL, blank=True, null=True)
room_type = models.ForeignKey("asset_app.Room_type", on_delete=models.SET_NULL, blank=True, null=True)
# Fields
name = models.CharField(max_length=30)
image = models.ImageField(storage=PublicMediaStorage(), null=True, blank=True)
我得到的错误是:
worksheet_s.insert_image(5, thisColumn, str('https://unord-tools-django-project-static.s3.eu-central-1.amazonaws.com/media/public/'+image))
TypeError: can only concatenate str (not "ImageFieldFile") to str发布于 2021-08-05 17:11:13
XlsxWriter不直接从那样的urls插入图像。您需要先读取数据。就像这样:
from io import BytesIO
from urllib.request import urlopen
import xlsxwriter
# Create the workbook and add a worksheet.
workbook = xlsxwriter.Workbook('image.xlsx')
worksheet = workbook.add_worksheet()
# Read an image from a remote url.
url = 'https://unord-tools-django-project-static.s3.eu-central-1.amazonaws.com/media/public/10e8f47bb84901d20ff435071577c58b_TFxmjcV.jpg'
image_data = BytesIO(urlopen(url).read())
# Write the byte stream image to a cell. Note, a dummy filename
# or description must be specified, or use a blank string.
worksheet.insert_image('B2', 'image name', {'image_data': image_data})
workbook.close()输出

https://stackoverflow.com/questions/68670095
复制相似问题