Python 求每月的天数

返回当月的实际天数

Python 求每月的天数_第1张图片

import calendar

# 第二个参数:当月天数
m = calendar.monthrange(2023,5)
print(m)  # (0, 31)
print(m[1])  # 31

 写入Excel

import calendar
import openpyxl
wb = openpyxl.load_workbook(r"C:\Users\HP\Desktop\日历.xlsx")
ws = wb.active
max_row = ws.max_row
max_col = ws.max_column
for i in range(2,max_row+1):
    y = int(ws.cell(i,2).value)
    m = int(ws.cell(i,4).value)
    ws.cell(i,10).value = calendar.monthrange(y,m)[1]

wb.save(r"C:\Users\HP\Desktop\日历.xlsx")

你可能感兴趣的:(Python,python,开发语言)