python制作日历并保存成excel_python台历代码--涉及知识点为Excel表格合并等操作

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(2020, i))):

for k in range(len(calendar.monthcalendar(2020, i)[j])):

value = calendar.monthcalendar(2020, 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')

# 添加图片

#循环文件夹中的图片,i为月份,每个月匹配一张图片

img =Image('C:/Users/DELL/Desktop/tu/%d.jpg' % i )

sheet.add_image(img, 'I1')

# 添加年份及月份

sheet.cell(row=3, column=1).value = '2020年'

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

# 设置周六周日显示红色

sheet.cell(row=9, column=1 ).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=10, column=1 ).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=11, column=1 ).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=12, column=1 ).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=13, column=1 ).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=14, column=1).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=9, column=7 ).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=10, column=7 ).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=11, column= 7).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=12, column= 7).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=13, column= 7).font = Font(u'微软雅黑', color='FF7887')

sheet.cell(row=14, column=7).font = Font(u'微软雅黑', color='FF7887')

# 保存文档

wb.save(r'C:\Users\DELL\Desktop\图片音视频\日历.xlsx')

注:本文参考https://mp.weixin.qq.com/s/lUGVq5z6WE7bvPS9HBy2uA 代码,做了少许改动,添加循环图片与周末颜色红色高亮显示,若有侵权,请联系我删除,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值