使用python 输出markdown日历

使用python 输出markdown日历

本文采用知识共享署名 4.0 国际许可协议进行许可,转载时请注明原文链接,图片在使用时请保留全部内容,可适当缩放并在引用处附上图片所在的文章链接。

需求:使用markdown 做日程计划的时候,需要生成相关的日历,这里程序输入年份和月份可以对应生成markdown 格式的日历。
环境要求: python 3
代码执行 :python markdown_schedule.py

# coding=utf-8

def is_leap_year(year):
    # 判断是否为闰年
    if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
        return True
    else:
        return False


def get_num_of_days_in_month(year, month):
    # 给定年月返回月份的天数
    if month in (1, 3, 5, 7, 8, 10, 12):
        return 31
    elif month in (4, 6, 9, 11):
        return 30
    elif is_leap_year(year):
        return 29
    else:
        return 28


def get_total_num_of_day(year, month):
    # 自1800年1月1日以来过了多少天
    days = 0
    for y in range(1800, year):
        if is_leap_year(y):
            days += 366
        else:
            days += 365

    for m in range(1, month):
        days += get_num_of_days_in_month(year, m)

    return days


def get_start_day(year, month):
    # 返回当月1日是星期几,由1800.01.01是星期三推算
    return 3 + get_total_num_of_day(year, month) % 7


# 月份与名称对应的字典
month_dict = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June',
              7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'}


def get_month_name(month):
    # 返回当月的名称
    return month_dict[month]


def print_month_title(year, month):
    # 打印日历的首部

    cal.write('         ' + str(get_month_name(month)) +  '   ' + str(year) + '          \n')
    cal.write('Sun | Mon | Tue  | Wed | Thu | Fri | Sat \n')
    cal.write('---| ---| ---| ---| ---| ---| ---|\n')


def print_month_body(year, month):
    '''
    打印日历正文
    格式说明:空两个空格,每天的长度为5
    需要注意的是print加逗号会多一个空格
    '''
    i = get_start_day(year, month)
    if i != 7:
        # cal.write(' ') # 打印行首的两个空格
        cal.write('  |' * (i %7))   # 从星期几开始则空5*几个空格
    for j in range(1, get_num_of_days_in_month(year, month)+1):
        cal.write(' [' + str(j) + '](#' + str(month) + str(j) + ') |')# 宽度控制,4+1=5
        i += 1
        if i % 7 == 0:  # i用于计数和换行
            cal.write('\n')   # 每换行一次行首继续空格



#   主函数部分
year = int(input("Please input target year:"))
month = int(input("Please input target month:"))
cal = open(str(year) + '-' + str(month) + '-日历markdown版.txt','w')
print_month_title(year, month)
print_month_body(year, month)
cal.close()

效果:

     July   2021          
SunMonTueWedThuFriSat
123
45678910
11121314151617
18192021222324
25262728293031
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要将Python输出的内容保存为Markdown格式,你可以使用以下代码示例: ```python import codecs # 要保存的Markdown文件路径 output_file = 'output.md' # 要输出的内容 content = """ # 标题 这是一段Markdown格式的文本。 - 列表项1 - 列表项2 - 列表项3 """ # 将内容写入Markdown文件 with codecs.open(output_file, 'w', 'utf-8') as f: f.write(content) ``` 在上述代码中,你可以将要输出Markdown内容保存在`content`变量中,然后使用`codecs.open`函数将内容写入指定的Markdown文件路径`output_file`中。 请注意,上述代码示例中的内容仅作为示范,你可以根据实际需求修改和扩展内容。 #### 引用[.reference_title] - *1* [Python HTML页面解析大全之如何使用xpath从页面提取信息wwdc2022并输出markdown和excel](https://blog.csdn.net/iCloudEnd/article/details/125165851)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [PDFkit用python批量把markdown格式文件导出成pdf文件代码](https://blog.csdn.net/weixin_35757191/article/details/128867983)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

002237

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值