2024-02-04 17:37:48 +00:00
|
|
|
|
import textwrap
|
|
|
|
|
|
2024-02-04 09:11:42 +00:00
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
2024-02-04 17:37:48 +00:00
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
|
|
def add_text(text_item):
|
|
|
|
|
|
|
|
|
|
for key, value in text_item.items():
|
|
|
|
|
i = 1
|
|
|
|
|
for text in value:
|
|
|
|
|
if type(text[0]) == str:
|
|
|
|
|
image = Image.open(r'D:\cori\员工奖励bg1.png')
|
|
|
|
|
draw = ImageDraw.Draw(image)
|
|
|
|
|
font_path_title = r'D:\onedrive\Documents\WeChat Files\fumeng0108\FileStorage\File\2020-05\思源黑体(7款)\思源黑体(7款)\SourceHanSansCN-Bold.otf'
|
|
|
|
|
font_path_content = r'D:\onedrive\Documents\WeChat Files\fumeng0108\FileStorage\File\2020-05\思源黑体(7款)\思源黑体(7款)\SourceHanSansCN-Regular.otf'
|
|
|
|
|
font_size_title = 36
|
|
|
|
|
font_size_content = 25
|
2024-02-04 09:11:42 +00:00
|
|
|
|
|
2024-02-04 17:37:48 +00:00
|
|
|
|
font_title = ImageFont.truetype(font_path_title, font_size_title)
|
|
|
|
|
font_content = ImageFont.truetype(font_path_content, font_size_content)
|
|
|
|
|
i += 1
|
|
|
|
|
# print(text)
|
|
|
|
|
# print(text[0])
|
|
|
|
|
# print(text[1])
|
|
|
|
|
# print(text[2])
|
|
|
|
|
# print(text[3])
|
|
|
|
|
# print(text[4])
|
|
|
|
|
# print(text[5])
|
|
|
|
|
save_name = key + text[0] + str(i) + '.png'
|
|
|
|
|
print(save_name)
|
|
|
|
|
# 获取文本大小
|
|
|
|
|
text_width1, text_height1 = draw.textsize(text[0], font_title)
|
|
|
|
|
text_width2, text_height2 = draw.textsize(text[1], font_content)
|
|
|
|
|
text_width3, text_height3 = draw.textsize(text[2], font_content)
|
|
|
|
|
text_width4, text_height4 = draw.textsize(str(text[3]), font_content)
|
|
|
|
|
text_width5, text_height5 = draw.textsize(str(text[4]), font_content)
|
2024-02-04 09:11:42 +00:00
|
|
|
|
|
2024-02-04 17:37:48 +00:00
|
|
|
|
# 计算文本的位置使其居中
|
|
|
|
|
image_width, a = image.size
|
|
|
|
|
text_x1 = (image_width - text_width1) / 2
|
|
|
|
|
text_x2 = (image_width - text_width2) / 2
|
|
|
|
|
text_x3 = (image_width - text_width3) / 2
|
|
|
|
|
text_x4 = (image_width - text_width4) / 2
|
|
|
|
|
text_x5 = (image_width - text_width5) / 2
|
2024-02-04 09:11:42 +00:00
|
|
|
|
|
2024-02-04 17:37:48 +00:00
|
|
|
|
# 在图片上添加文本
|
|
|
|
|
draw.text((text_x1, 19), text[0], font=font_title, fill="#AA1017")
|
|
|
|
|
draw.text((text_x2, 186), text[1], font=font_content, fill="#FAC79B")
|
|
|
|
|
draw.text((text_x3, 346), text[2], font=font_content, fill="#FAC79B")
|
|
|
|
|
draw.text((text_x4, 505), str(text[3]), font=font_content, fill="#FAC79B")
|
|
|
|
|
draw.text((text_x5, 664), str(text[4]), font=font_content, fill="#FAC79B")
|
|
|
|
|
# text_width = 22
|
|
|
|
|
# wrapped_text = textwrap.wrap(text[5], width=text_width)
|
|
|
|
|
# pos = (36, 868)
|
|
|
|
|
# leading = 42
|
|
|
|
|
# for i, line in enumerate(wrapped_text):
|
|
|
|
|
# draw.text((pos[0], leading * i + pos[1]), line, font=font_content, fill="#FAC79B")
|
2024-02-04 09:11:42 +00:00
|
|
|
|
|
2024-02-04 17:37:48 +00:00
|
|
|
|
# 保存修改后的图片
|
|
|
|
|
image.save(r"D:\cori\员工营销" + "/" + save_name)
|
2024-02-04 09:11:42 +00:00
|
|
|
|
|
2024-02-04 17:37:48 +00:00
|
|
|
|
def get_data(file_path):
|
|
|
|
|
# 读取Excel文件
|
|
|
|
|
excel_data = pd.read_excel(file_path, sheet_name=None)
|
2024-02-04 09:11:42 +00:00
|
|
|
|
|
2024-02-04 17:37:48 +00:00
|
|
|
|
# 将每个sheet的数据转换成字典
|
|
|
|
|
data_dict = {}
|
|
|
|
|
for sheet_name, sheet_data in excel_data.items():
|
|
|
|
|
data_dict[sheet_name] = sheet_data.values.tolist()
|
2024-02-04 09:11:42 +00:00
|
|
|
|
|
2024-02-04 17:37:48 +00:00
|
|
|
|
return data_dict
|
|
|
|
|
# print(data_dict['全员'][0])
|
2024-02-04 09:11:42 +00:00
|
|
|
|
|
2024-02-04 17:37:48 +00:00
|
|
|
|
file_path = r'D:\cori\2024年全员营销奖励一张表 - 营销助手展示-拆分.xlsx'
|
|
|
|
|
text_item = get_data(file_path)
|
2024-02-04 09:11:42 +00:00
|
|
|
|
|
2024-02-04 17:37:48 +00:00
|
|
|
|
add_text(text_item, )
|