N
N
new20leon2020-10-27 18:58:16
Python
new20leon, 2020-10-27 18:58:16

How to get (determine) the last filled cell in excel as a variable?

Information is of interest from 3 cells in BC D. It may be that, for example, cell B is empty, but D has information

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/upload... "
im_url_andrey = " https://ashevchenko.kiev.ua/assets/images/a-shevch... "
im_url_yulya = "stuki-druki.com/aforizms/Yulia-Tymoshenko-01.jpg "

size = (200, 200)
wb = Workbook()
ws = wb.active

insert_row(ws, im_url_nastya, "Nastya", 1, size=size)
insert_row (ws, im_url_andrey, "Andrey", 2, size=size)
insert_row(ws, im_url_yulya, "Julia", 3, size=size)

wb.save('c:/test.xlsx')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question