首页 > 编程语言 > 详细

python台历代码--涉及知识点为Excel表格合并等操作

时间:2020-01-06 19:46:16      阅读:94      评论:0      收藏:0      [点我收藏+]
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 代码,做了少许改动,添加循环图片与周末颜色红色高亮显示,若有侵权,请联系我删除,谢谢

python台历代码--涉及知识点为Excel表格合并等操作

原文:https://www.cnblogs.com/xixirang/p/12157752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!