用Python自动化生成胡歌爱豆日历(文末附赠python教材分享)
本次内容有感于《Python编程快速上手-让繁琐工作自动化》。
根据书中的「处理Excel电子表格」章节内容,做出一份专属日历。
使用的模块为openpyxl,一个能读取和修改Excel电子表格的Pyhton模块。
实现自动化处理表格信息,摆脱无趣无味。
此外还有calendar模块,通过该模块生成日历信息。
最后利用openpyxl和calendar库,实现自动化生成爱豆日历。
/ 01 / 科普
在进行代码操作前,简单对相关知识做个简单的学习。
一个Excel电子表格文档称为一个工作䈬。
一个工作䈬保存在扩展名为「.xlsx」的文件中。
划重点,openpyxl只支持「.xlsx」类型的格式。
所以对于Excel2003版「.xls」格式是不支持的。
每个工作䈬可以包含多个表,又称为工作表。
本次生成的信息就是在一个工作䈬,12个工作表内。
calendar库主要是生成日历信息。
import calendar
# 设置每周的起始日期码,为星期天
calendar.setfirstweekday(firstweekday=6)
# 返回2019年年历
print(calendar.calendar(2019, w=2, l=1, c=6))
输出如下。

返回1月份每列信息情况。
# 返回每月每列信息
print(calendar.monthcalendar(2019, 1))
# 输出结果
[[0, 0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, 0, 0]]
与上图里1月份信息比较下,你会发现多了个数字0。
其他完全一样,所以遍历列表信息就完事了。
/ 02 / 爱豆日历
爱豆日历的生成其实也挺简单的。
主要是单元格文本添加,字体设置,背景设置,图片设置。
详细代码如下。
from openpyxl.styles import Alignment, PatternFill, Font
from openpyxl.utils import get_column_letter
from openpyxl.drawing.image import Image
import openpyxl
import calendar
# 设置第一天为星期天
calendar.setfirstweekday(firstweekday=6)
# 创建一个工作䈬
wb = openpyxl.Workbook()
# 遍历12个月
for i in range(1, 13):
# 添加工作表
sheet = wb.create_sheet(index=0, title=str(i) + '月')
# 获取具体日期时间
for j in range(len(calendar.monthcalendar(2019, i))):
for k in range(len(calendar.monthcalendar(2019, i)[j])):
value = calendar.monthcalendar(2019, i)[j][k]
# 将0值变为空值
if value == 0:
value = ''
sheet.cell(row=j + 9, column=k + 1).value = value
else:
sheet.cell(row=j + 9, column=k + 1).value = value
# 设置字体
sheet.cell(row=j + 9, column=k + 1).font = Font(u'微软雅黑', size=11)
# 单元格文字设置,右对齐,垂直居中
align = Alignment(horizontal='right', vertical='center')
# 单元格填充色属性设置
fill = PatternFill("solid", fgColor="B9EBF7")
# 对单元格进行颜色填充
for k1 in range(1, 100):
for k2 in range(1, 100):
sheet.cell(row=k1, column=k2).fill = fill
# 添加星期几信息行
days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
num = 0
for k3 in range(1, 8):
sheet.cell(row=8, column=k3).value = days[num]
sheet.cell(row=8, column=k3).alignment = align
sheet.cell(row=8, column=k3).font = Font(u'微软雅黑', size=11)
# 设置列宽12
c_char = get_column_letter(k3)
sheet.column_dimensions[get_column_letter(k3)].width = 12
num += 1
# 设置行高30
for k4 in range(8, 14):
sheet.row_dimensions[k4].height = 30
# 合并单元格
sheet.merge_cells('I1:P20')
# 添加图片
img = Image('huge_2.jpg')
sheet.add_image(img, 'I1')
# 添加年份及月份
sheet.cell(row=3, column=1).value = '2019年'
sheet.cell(row=4, column=1).value = str(i) + '月'
# 设置年份及月份文本属性
sheet.cell(row=3, column=1).font = Font(u'微软雅黑', size=16, bold=True, color='FF7887')
sheet.cell(row=4, column=1).font = Font(u'微软雅黑', size=16, bold=True, color='FF7887')
sheet.cell(row=3, column=1).alignment = align
sheet.cell(row=4, column=1).alignment = align
# 保存文档
wb.save('爱豆日历.xlsx')
相关图片文件已上传网盘,公众号回复「爱豆」即可获取。
最后来个小预览。

原来小F的爱豆是胡歌,那么你的又是谁。
还不去动动手,做出属于自己的爱豆日历。
这里小F由于时间关系,忘记给星期六、星期日这两列更改字体颜色。
小伙伴们在尝试的时候,记得更改!!!
/ 03 / 总结
本次的成果如下。












嗯,12个月,都有爱豆的陪伴。
这种体验真的好~
万水千山总是情,转发「一下」行不行。
最后,想学习Python的小伙伴们!
请关注+私信回复:“学习”就可以拿到一份我为大家准备的Python学习资料!

