Η κατεύθυνση
Ο κώδικας python:
—
# python script: create_quote.py
import os
from PIL import Image, ImageDraw, ImageFont, ImageOps
from openpyxl import Workbook, load_workbook
from openpyxl.drawing.image import Image as XLImage
import textwrap
# ———————————–
# 1. GET MULTI-LINE TEXT FROM USER
# ———————————–
print(“Enter your quote. Type ‘submit’ on a new line to finish:”)
lines = []
while True:
line = input()
if line.strip().lower() == “submit”:
break
# PRESERVE EMPTY LINES
lines.append(line)
quote_text = “\n”.join(lines)
# ———————————–
# 2. FILE CHECK: DOES quote.png EXIST?
# ———————————–
base_name = “quote”
ext = “.png”
filename = base_name + ext
if os.path.exists(filename):
print(f”\nFile ‘{filename}’ already exists.”)
print(“What do you want to do?”)
print(“1 = Overwrite existing file”)
print(“2 = Create a new PNG (quote_1.png, quote_2.png, …)”)
choice = input(“Choose 1 or 2: “)
if choice == “1”:
png_path = filename
else:
# find next numbered file
n = 1
while os.path.exists(f”{base_name}_{n}{ext}”):
n += 1
png_path = f”{base_name}_{n}{ext}”
print(f”New file will be saved as: {png_path}”)
else:
png_path = filename
# ———————————–
# 3. IMAGE SETTINGS
# ———————————–
width, height = 1200, 1600
background_color = (240, 240, 240)
text_color = (60, 60, 60)
corner_radius = 50
font_size = 52
# Try loading a font
try:
font = ImageFont.truetype(“arial.ttf”, font_size)
except:
font = ImageFont.load_default()
# ———————————–
# 4. CREATE ROUNDED RECT IMAGE
# ———————————–
rounded_rect = Image.new(“RGBA”, (width, height), background_color)
mask = Image.new(“L”, (width, height), 0)
draw_mask = ImageDraw.Draw(mask)
draw_mask.rounded_rectangle((0, 0, width, height), radius=corner_radius, fill=255)
final_img = Image.new(“RGBA”, (width, height), (255, 255, 255, 0))
final_img.paste(rounded_rect, (0, 0), mask)
# ———————————–
# 5. DRAW TEXT
# ———————————–
draw = ImageDraw.Draw(final_img)
wrapped = []
for line in quote_text.split(“\n”):
if line.strip() == “”:
wrapped.append(“”) # PRESERVE EMPTY LINE
else:
wrapped.extend(textwrap.wrap(line, width=30))
final_text = “\n”.join(wrapped)
bbox = draw.multiline_textbbox((0, 0), final_text, font=font, spacing=12)
text_w = bbox[2] – bbox[0]
text_h = bbox[3] – bbox[1]
x = (width – text_w) // 2
y = (height – text_h) // 2
draw.multiline_text(
(x, y),
final_text,
font=font,
fill=text_color,
spacing=12,
align=”center”
)
# ———————————–
# 6. SAVE THE PNG
# ———————————–
final_img.save(png_path)
print(f”\nPNG saved as: {png_path}”)
# ———————————–
# 7. INSERT IMAGE INTO EXCEL (STACK UNDER EXISTING IMAGES)
# ———————————–
excel_path = “quote_excel.xlsx”
# Load or create workbook
if os.path.exists(excel_path):
wb = load_workbook(excel_path)
ws = wb.active
else:
wb = Workbook()
ws = wb.active
# Determine the lowest existing image bottom position
max_row = 1
for img in ws._images:
anchor_cell = img.anchor._from # internal anchor information
img_row = anchor_cell.row # row where image begins
height_rows = int((img.height / 20)) # convert pixel height → approx row height
bottom_row = img_row + height_rows + 2
if bottom_row > max_row:
max_row = bottom_row
# Insert new image exactly under the previous one
excel_img = XLImage(png_path)
ws.add_image(excel_img, f”B{max_row}”)
wb.save(excel_path)
print(f”Excel updated → {excel_path}”)
Views: 2
Comments are closed.