Weekly Report Template Creation

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to create a weekly report template for a new project team. The weekly report is used to summarize the daily work of the project team for regular review.

👀 Preview

## example

## start_date = datetime.date(2023,10,1)      Sunday
## end_date = datetime.date(2023,10,16)       Monday
$ ls weekly
2023-10-06.txt  2023-10-13.txt  2023-10-16.txt
$ cat 2023-10-06.txt
2023-10-02
2023-10-03
2023-10-04
2023-10-05
2023-10-06
$ cat 2023-10-16.txt
2023-10-16

🎯 Tasks

In this project, you will learn:

  • How to create a "weekly" folder to store the weekly report template files
  • How to determine the Friday of the week containing the start date
  • How to write the weekly report template files in the "weekly" folder
  • How to adjust the end date to the previous Friday if it falls on a Saturday or Sunday
  • How to create the final weekly report template file for the last week of the project

🏆 Achievements

After completing this project, you will be able to:

  • Automatically generate weekly report templates based on the project start and end dates
  • Ensure the weekly report templates only include weekdays, excluding weekends
  • Adjust the end date to the previous Friday if it falls on a weekend
  • Create a consistent and organized structure for storing the weekly report templates

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/FileHandlingGroup -.-> python/file_reading_writing("`Reading and Writing Files`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") python/PythonStandardLibraryGroup -.-> python/date_time("`Date and Time`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/with_statement -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/booleans -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/conditional_statements -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/for_loops -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/while_loops -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/tuples -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/function_definition -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/importing_modules -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/standard_libraries -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/file_opening_closing -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/file_reading_writing -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/iterators -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/date_time -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/os_system -.-> lab-302784{{"`Weekly Report Template Creation`"}} python/build_in_functions -.-> lab-302784{{"`Weekly Report Template Creation`"}} end

Create the weekly Folder

In this step, you will learn how to create the "weekly" folder to store the weekly report template files.

  1. Open the report.py file in your code editor.
  2. Locate the create function in the code.
  3. Inside the create function, add the following code to create the "weekly" folder:
import os

def create(start_date, end_date):
    ## Create the "weekly" folder
    os.makedirs("weekly", exist_ok=True)

    ## Rest of the code...

The os.makedirs("weekly", exist_ok=True) line creates the "weekly" folder in the same directory as the report.py file. The exist_ok=True parameter ensures that the folder is created even if it already exists, preventing an error.

Determine the Friday of the Start Date’s Week

In this step, you will learn how to find the Friday of the week containing the start date.

  1. In the create function, add the following code after the folder creation:
import datetime

def create(start_date, end_date):
    ## Create the "weekly" folder
    os.makedirs("weekly", exist_ok=True)

    ## Find the Friday of the week containing the start date
    current_date = start_date
    while current_date.weekday() != 4:  ## 4 represents Friday
        current_date += datetime.timedelta(days=1)

    ## Rest of the code...

The code uses a while loop to iterate through the days starting from the start date until the current date is a Friday (weekday 4). This ensures that the current_date variable is set to the Friday of the week containing the start date.

Write the Weekly Report Template Files

In this step, you will learn how to write the weekly report template files in the "weekly" folder.

  1. In the create function, add the following code after finding the Friday of the start date's week:
def create(start_date, end_date):
    ## Create the "weekly" folder
    os.makedirs("weekly", exist_ok=True)

    ## Find the Friday of the week containing the start date
    current_date = start_date
    while current_date.weekday() != 4:  ## 4 represents Friday
        current_date += datetime.timedelta(days=1)

    ## Traverse the date range starting from the Friday of the start date's week
    while current_date <= end_date:
        ## Construct the file name
        file_name = current_date.strftime("%Y-%m-%d") + ".txt"
        file_path = os.path.join("weekly", file_name)

        ## Write file content
        with open(file_path, "w") as file:
            ## Write the range of weekdays from the start date to the current Friday (excluding weekends)
            date = max(
                start_date, current_date - datetime.timedelta(days=6)
            )  ## Ensure the start date is no earlier than one week before the current Friday

            while date <= current_date:
                if date.weekday() < 5:  ## 0 represents Monday, 4 represents Friday
                    file.write(date.strftime("%Y-%m-%d") + "\n")
                date += datetime.timedelta(days=1)

        ## Update the current Friday to the next Friday
        current_date += datetime.timedelta(days=7)

    ## Rest of the code...

The code uses a while loop to traverse the date range starting from the Friday of the start date's week until the end date. For each Friday, it creates a weekly report template file in the "weekly" folder with the date of the Friday as the file name.

Inside each file, the code writes the range of weekdays from the start date (or one week before the current Friday, whichever is later) to the current Friday, excluding weekends.

Adjust the End Date to the Previous Friday if Necessary

In this step, you will learn how to adjust the end date to the previous Friday if it falls on a Saturday or Sunday.

  1. In the create function, add the following code after the loop that writes the weekly report template files:
def create(start_date, end_date):
    ## Create the "weekly" folder
    os.makedirs("weekly", exist_ok=True)

    ## Find the Friday of the week containing the start date
    ## ... (previous steps)

    ## Traverse the date range starting from the Friday of the start date's week
    ## ... (previous step)

    ## Adjust the end date to the previous Friday if it falls on a Saturday or Sunday
    if end_date.weekday() == 5:  ## 5 represents Saturday
        end_date -= datetime.timedelta(days=1)
    elif end_date.weekday() == 6:  ## 6 represents Sunday
        end_date -= datetime.timedelta(days=2)

    ## Rest of the code...

The code checks if the end date falls on a Saturday (weekday 5) or Sunday (weekday 6). If so, it adjusts the end date to the previous Friday by subtracting 1 or 2 days, respectively.

This ensures that the weekly report template for the final week only includes the weekdays from Monday to the adjusted end date, excluding weekends.

Create the Final Weekly Report Template File

In this step, you will learn how to create the weekly report template file for the final week, starting from the Monday of the week containing the adjusted end date.

  1. In the create function, add the following code after adjusting the end date:
def create(start_date, end_date):
    ## Create the "weekly" folder
    os.makedirs("weekly", exist_ok=True)

    ## Find the Friday of the week containing the start date
    ## ... (previous steps)

    ## Traverse the date range starting from the Friday of the start date's week
    ## ... (previous steps)

    ## Adjust the end date to the previous Friday if it falls on a Saturday or Sunday
    ## ... (previous step)

    ## Create a submission for the week from Monday to the adjusted end date
    ## Find the Monday of the week containing the adjusted end date
    current_date = end_date

    ## Construct the file name
    file_name = current_date.strftime("%Y-%m-%d") + ".txt"
    file_path = os.path.join("weekly", file_name)

    while current_date.weekday() != 0:  ## 0 represents Monday
        current_date -= datetime.timedelta(days=1)

    ## Write file content
    with open(file_path, "w") as file:
        ## Write the range of weekdays from Monday to the adjusted end date (excluding weekends)
        date = current_date
        while date <= end_date:
            if date.weekday() < 5:  ## 0 represents Monday, 4 represents Friday
                file.write(date.strftime("%Y-%m-%d") + "\n")
            date += datetime.timedelta(days=1)

The code first finds the Monday of the week containing the adjusted end date. It then constructs the file name for the final weekly report template and opens the file for writing.

Inside the file, the code writes the range of weekdays from Monday to the adjusted end date, excluding weekends.

This ensures that the final weekly report template file only includes the weekdays from the Monday of the week containing the adjusted end date to the adjusted end date itself.

With these steps above, you have completed the implementation of the create function in the report.py file, which generates the weekly report templates based on the provided start and end dates.

  1. To generate the weekly report templates, run the report.py script with the python report.py command in your terminal. The script will create the weekly report templates based on the provided start and end dates.

Some examples are provided below:

## example 1

## start_date = datetime.date(2023,10,1)      Sunday
## end_date = datetime.date(2023,10,16)       Monday
$ ls weekly
2023-10-06.txt  2023-10-13.txt  2023-10-16.txt
$ cat 2023-10-06.txt
2023-10-02
2023-10-03
2023-10-04
2023-10-05
2023-10-06
$ cat 2023-10-16.txt
2023-10-16

## example 2

## start_date = datetime.date(2023,9,10)     Sunday
## end_date = datetime.date(2023,9,24)       Sunday
$ ls weekly
2023-09-15.txt  2023-09-22.txt
$ cat 2023-09-15.txt
2023-09-11
2023-09-12
2023-09-13
2023-09-14
2023-09-15
$ cat 2023-09-22.txt
2023-09-18
2023-09-19
2023-09-20
2023-09-21
2023-09-22

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like