Note2024092801_python 日历信息获取
在日历中找标记的日期并保存
1.背景
2.解读视频链接
后续更新后,会放在评论区,感谢大家关注。
3.代码实现过程
代码如下:
'''
刚刚实现了一个python项目
关于 日历中工作日 信息读取的python程序
通过这个程序可以 熟悉 python 对excel表的读取和 写入
另一方面也为项目的开展做一方面的准备
话不多说,正题开始:
程序已经经过测试,可以实现预期效果
'''
from openpyxl import load_workbook
import openpyxl
from openpyxl.styles.colors import Color
def monthJudge(rowNum,column):
if rowNum>=1 and rowNum<=8:
if column >=1 and column<=7:
month = 1
elif column >=9 and column<=15:
month = 2
elif column >= 17 and column <= 23:
month = 3
else:
month = 4
elif rowNum>=13 and rowNum<=18:
if column >=1 and column<=7:
month = 5
elif column >=9 and column<=15:
month = 6
elif column >= 17 and column <= 23:
month = 7
else:
month = 8
else:
if column >=1 and column<=7:
month = 9
elif column >=9 and column<=15:
month = 10
elif column >= 17 and column <= 23:
month = 11
else:
month = 12
return month
COLOR_INDEXED = {0: '00000000', 1: '00FFFFFF', 2: '00FF0000', 3: '0000FF00', 4: '000000FF',
5: '00FFFF00', 6: '00FF00FF', 7: '0000FFFF', 8: '00000000', 9: '00FFFFFF',
10: '00FF0000', 11: '0000FF00', 12: '000000FF', 13: '00FFFF00', 14: '00FF00FF',
15: '0000FFFF', 16: '00800000', 17: '00008000', 18: '00000080', 19: '00808000',
20: '00800080', 21: '00008080', 22: '00C0C0C0', 23: '00808080', 24: '009999FF',
25: '00993366', 26: '00FFFFCC', 27: '00CCFFFF', 28: '00660066', 29: '00FF8080',
30: '000066CC', 31: '00CCCCFF', 32: '00000080', 33: '00FF00FF', 34: '00FFFF00',
35: '0000FFFF', 36: '00800080', 37: '00800000', 38: '00008080', 39: '000000FF',
40: '0000CCFF', 41: '00CCFFFF', 42: '00CCFFCC', 43: '00FFFF99', 44: '0099CCFF',
45: '00FF99CC', 46: '00CC99FF', 47: '00FFCC99', 48: '003366FF', 49: '0033CCCC',
50: '0099CC00', 51: '00FFCC00', 52: '00FF9900', 53: '00FF6600', 54: '00666699',
55: '00969696', 56: '00003366', 57: '00339966', 58: '00003300', 59: '00333300',
60: '00993300', 61: '00993366', 62: '00333399', 63: '00333333', 64: 'System Foreground', 65: 'System Background'
}
print("COLOR_INDEXED[0]:", COLOR_INDEXED[0])
# 新建一个表,用来存放数据
myNewWorkBook = openpyxl.Workbook()
sheet = myNewWorkBook.active
# 打开Excel文件
workbook = load_workbook('E:/Python/Pycharm/Z05_Calendar/2024年日历中英文版.xlsx')
# print(type(workbook.sheetnames), workbook.sheetnames) # 从这里可以看到工作薄工作表中的内容;运行结果 <class 'list'> ['中文', '英文']
myWorksheet = workbook.worksheets[0] # 获得第一张工作表及其所有内容;
# print(type(myWorksheet.title), myWorksheet.title) #运行结果:<class 'str'> 中文
# print(myWorksheet['A1'].value) # 运行结果:上海发那科2024年日历
# rows = myWorksheet.max_row # 获取总共有多少行
# print(rows) # 运行结果:46
myRows = 26
myColumn = myWorksheet.max_column
print(myColumn)
flag = 0
writeResult = []
for i in range(1, myRows):
for j in range(1, myColumn):
ce1 = myWorksheet.cell(row=i, column=j)
cellValue = myWorksheet.cell(row=i, column=j).value
if cellValue != None:
myCellFill = ce1.fill
if "theme" == myCellFill.start_color.type:
# 颜色使用背景颜色
cell_color = myCellFill.start_color.theme
elif "auto" == myCellFill.start_color.type:
# 颜色使用自动颜色
cell_color = 'Automatic'
elif "indexed" == myCellFill.start_color.type:
# 颜色使用索引颜色
cell_color = myCellFill.start_color.indexed
cellBgColor = COLOR_INDEXED[cell_color]
else:
# 颜色使用rgb颜色
cell_color = myCellFill.start_color.rgb
if cell_color != "00000000" and cellValue != "六" and cellValue != "日":
myMonth = monthJudge(i, j)
targetValue = cellValue
myResult = str(myMonth)+"月"+str(targetValue)+"日"
print(myResult)
writeResult.append(myResult)
for sheetRow in range(1, len(writeResult)):
writeResultList = [writeResult[sheetRow]]
sheet.append(writeResultList)
myNewWorkBook.save('E:/Python/Pycharm/Z05_Calendar/2024年假期.xlsx')
以上