以插入遠(yuǎn)程圖片為例子
from fake_useragent import UserAgent
fixed_size = 36 # 固定size為36
name = "test.xlsx"
url = "https://xxx/xxx.png"
workbook = xlsxwriter.Workbook(name)
worksheet = self.workbook.add_worksheet()
ua = UserAgent()
headers = {
'User-Agent': ua.random,
}
content = requests.get(url, headers=headers).content
image_data = io.BytesIO(content)
img = Image.open(image_data)
print("尺寸:",img.size)
x = float(fixed_size / img.size[0]) # 因?yàn)闇y(cè)試圖片長(zhǎng)和寬相等赫段,只計(jì)算一邊
self.worksheet.insert_image(f'A{row}', url, {'x_scale': x, 'y_scale': x, 'image_data': image_data,'valign': 'vcenter', 'align': 'center'})