创建变量以根据天数建立Excel工作表

时间:2019-03-04 03:42:51

标签: python datetime environment-variables

我希望在菜单上添加一个选项,以创建带有当前标题的工作簿标签。

我正在跑步:

wb=load_workbook(/path/)
# Create sheet based on day
date = today.strftime("%m/%d/%y")
wb.create_sheet(date)

#save changes
wb.save(filepath)

即时通讯:

File "/path", line 39, in menu
date = today.strftime("%m/%d/%y")

NameError: name 'today' is not defined

有什么想法吗?

更新: 当前代码:

        from openpyxl import load_workbook
        import os
        import datetime 

        wb=load_workbook("C:/path/)

        # Create sheet based on day
        today = datetime.date.today()     # <-- Define the `today` object

        #create worksheet
        wb.create_sheet(today)

        #save changes
        wb.save(filepath)

导致:

文件“ C:/ path /”,标题行93     m = INVALID_TITLE_REGEX.search(值)

TypeError:预期的字符串或类似字节的对象

1 个答案:

答案 0 :(得分:0)

如NameError所建议,today对象未定义。

我在下面的代码中添加了几行,应该会有所帮助。

            import datetime                     # <-- Ensure `datetime` module is imported
            wb=load_workbook(/path/)
            # Create sheet based on day
            today = datetime.datetime.now()     # <-- Define the `today` object
            date = today.strftime("%m/%d/%y")
            wb.create_sheet(date)

            #save changes
            wb.save(filepath)