输入:
1.一个包含身份证、姓名的excel表格。2.一张生日贺卡模板图片。
操作:
填入姓名、年龄、贺卡生成时间(设置为生日当天)。
输出:
一个文件夹,包含所有人当年的生日贺卡,生日贺卡命名为"发送贺卡当天日期_身份证号.png"。
from PIL import Image,ImageDraw,ImageFont
import time
import os
import pandas as pd
# import datetime
# today = datetime.datetime.now()
# today.year
# today.month
# today.day
def imgaddnum(img,info):
# 将img添加到画板
savepath = "C:\\Users\\用户名\\Desktop\\2022photoes\\"
imgdraw = ImageDraw.Draw(img)
# 设置需要绘制的字体 参数:字体名,字体大小
imgfont = ImageFont.truetype("C:\Windows\Fonts\simkai.ttf",size=30)
# 字体颜色
fillcolor = "#dd1c5c"
# 获取img的宽和高
imgw,imgh = img.size
name = info["name"]
birthday = str(info["id"])[6:14]
age = str(2022 - int(str(info["id"])[6:10]))
# 开始将文字内容绘制到img的画板上 参数:坐标,绘制内容,填充颜色,字体
imgdraw.text((imgw/2,0),name,fill=fillcolor,font=imgfont)
imgdraw.text((imgw/2,110),age,fill=fillcolor,font=imgfont)
imgdraw.text((imgw/2,300),"2022 "+str(info["id"])[10:12]+" "+ str(info["id"])[12:14],fill=fillcolor,font=imgfont)
# 设置img的保存路径和文件名
imgsavetarget = savepath +birthday+"_"+ str(info["id"]) + ".png"
# 开始保存
res = img.save(imgsavetarget)
# 返回保存结果
return res
def draw_card(info):
targetimg = None
sourceimg ="C:\\Users\\用户名\\Desktop\\birthday.jpg"
# 判断需要打开的img对象路径是否存在
if os.path.exists(sourceimg):
targetimg = Image.open(sourceimg)
rig = imgaddnum(targetimg,info)
print(rig)
else:
print("Image Not Found!")
filepath = "C:\\Users\\用户名\\Desktop\\info.et"
def main(filepath):
data = pd.DataFrame(pd.read_excel(filepath))
print(data)
for row in data.iterrows():
print(type(row[1]))
print(row)
print(row[1])
draw_card(row[1])
main(filepath)
# pd.DataFrame(pd.read_excel(excelFile))