Юленька Asked:2020-06-25 03:39:13 +0000 UTC2020-06-25 03:39:13 +0000 UTC 2020-06-25 03:39:13 +0000 UTC 在 Excel 或 Word 中解析和写入图片 772 有图片的链接直接有文字:text1,title1 excel、word或其他方式 如何下载图片并写入文本,以便它们在第一列的不同列中的一个文件中 1 张照片 \t 1 名称 \t text1 用于第二列:2 张照片 \t 2 名称 \t text2 python 1 个回答 Voted Best Answer MaxU - stop genocide of UA 2020-06-25T06:27:05Z2020-06-25T06:27:05Z 工作示例: from PIL import Image from io import BytesIO import requests import openpyxl from openpyxl import Workbook def get_img(url, size=(100, 100)): r = requests.get(url, stream=True) if not r.ok: r.raise_for_error() r.raw.decode_content = True img = Image.open(r.raw) if size: img = img.resize(size) temp = BytesIO() img.save(temp, format="png") temp.seek(0) return Image.open(temp) def insert_row(ws, img_url, name, num, size=(200,200)): img = openpyxl.drawing.image.Image(get_img(img_url, size=size)) row_num = ws.max_row + 1 cell_addr = f"A{row_num}" img.anchor = cell_addr ws.add_image(img) ws[f"B{row_num}"] = name ws[f"C{row_num}"] = num ws.row_dimensions[row_num].height = int(size[1] * .8) ws.column_dimensions["A"].width = int(size[0] * .2) ############################################################################## im_url_nastya = "https://s2.cdn.teleprogramma.pro/wp-content/uploads/2019/02/59d2d0c81e222b6a2dd496f448f454dd.jpg" im_url_andrey = "https://ashevchenko.kiev.ua/assets/images/a-shevchenko-2.jpg" im_url_yulya = "http://stuki-druki.com/aforizms/Yulia-Tymoshenko-01.jpg" size = (200, 200) wb = Workbook() ws = wb.active insert_row(ws, im_url_nastya, "Настя", 1, size=size) insert_row(ws, im_url_andrey, "Андрей", 2, size=size) insert_row(ws, im_url_yulya, "Юля", 3, size=size) wb.save('c:/temp/test.xlsx')
工作示例: