-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathten-weeks-of-dates.py
64 lines (48 loc) · 2.19 KB
/
ten-weeks-of-dates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from datetime import datetime, timedelta
from docx import Document
from docx.shared import Pt
from docx.shared import RGBColor
import holidays
def generate_date_range(start_date, weeks):
date_format = "%m/%d"
start_date = datetime.strptime(start_date, date_format)
# Create a new Word document
doc = Document()
# Add a table to the document
table = doc.add_table(rows=1, cols=1)
table.style = 'Table Grid'
table.autofit = False
# Initialize US holidays
us_holidays = holidays.UnitedStates(years=start_date.year)
for week in range(weeks):
end_date = start_date + timedelta(days=6)
week_dates = [start_date + timedelta(days=i) for i in range(7)]
# Check if any day within the week is a US holiday
week_holidays = [us_holidays[date] for date in week_dates if date in us_holidays]
date_range = f"{start_date.strftime(date_format)} - {end_date.strftime(date_format)}"
# Add the holiday with date if any day within the week is a holiday
if week_holidays:
holiday_date = [date for date in week_dates if date in us_holidays][0]
holiday_text = f"{holiday_date.strftime(date_format)}: {week_holidays[0]} (US Holiday)"
date_range += f"\n{holiday_text}"
# Insert a new row in the table
row = table.add_row().cells
cell_paragraph = row[0].paragraphs[0]
# Set font for all text in the cell to Times New Roman size 11
for run in cell_paragraph.runs:
run.font.name = 'Times New Roman'
run.font.size = Pt(11)
# # Set font color to red for holiday text
# if "US Holiday" in run.text:
# run.font.color.theme_color = 1 # 1 corresponds to the 'dark red' theme color
cell_paragraph.add_run(str(week + 1))
cell_paragraph.add_run("\n" + date_range)
start_date = end_date + timedelta(days=1)
# Save the document
doc.save('output.docx')
# User input
start_date_input = input("Enter the starting date (MM/DD): ")
weeks_to_generate = 10
# Generate and save the Word document
generate_date_range(start_date_input, weeks_to_generate)
print("saved as output.docx")