from datetime import date
from dateutil.relativedelta import relativedelta
import calendar
def generate_calendar_html(filename, start_date=date(2024, 7, 1), end_date=date(2025, 7, 1)):
"""
Generates an HTML calendar for each month with sentences from a text file.
Args:
filename: Path to the text file containing daily sentences (365 lines).
start_date: Start date of the calendar (default: July 1, 2024).
end_date: End date of the calendar (default: July 1, 2025).
"""
# Check file existence and length
try:
with open(filename, 'r') as f:
sentences = f.readlines()
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
return
if len(sentences) != 365:
print(f"Error: File should contain 365 lines (one for each day).")
return
current_date = start_date
html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calendar</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
width: 14.28%; /* 100% divided by 7 columns */
padding: 60px; /* Ensure cells are square */
text-align: center;
vertical-align: top;
position: relative;
}
.tooltip {
position: absolute;
display: none;
background-color: #f9f9f9;
border: 1px solid black;
padding: 5px;
z-index: 1;
width: 200px;
box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.1);
word-wrap: break-word;
}
</style>
<script>
function showTooltip(event, text) {
const tooltip = document.getElementById('tooltip');
tooltip.style.display = 'block';
tooltip.innerHTML = text;
tooltip.style.left = event.pageX + 10 + 'px';
tooltip.style.top = event.pageY + 10 + 'px';
}
function hideTooltip() {
const tooltip = document.getElementById('tooltip');
tooltip.style.display = 'none';
}
</script>
</head>
<body>
<div id="tooltip" class="tooltip"></div>
"""
while current_date < end_date:
month = current_date.month
year = current_date.year
num_days = calendar.monthrange(year, month)[1]
first_weekday = calendar.monthrange(year, month)[0] # 0 = Monday, 6 = Sunday
html_content += f"<h2>{calendar.month_name[month]} {year}</h2>"
html_content += """
<table>
<tr>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
<tr>
"""
day = 1
for _ in range(first_weekday):
html_content += "<td></td>"
while day <= num_days:
if (first_weekday + day - 1) % 7 == 0 and day != 1:
html_content += "</tr><tr>"
cell_date = date(year, month, day)
if cell_date >= start_date and cell_date < end_date:
day_index = (cell_date - start_date).days
sentence = sentences[day_index].strip()
html_content += f"""
<td onmouseover="showTooltip(event, '{sentence}')" onmouseout="hideTooltip()">
{day}
</td>
"""
day += 1
for _ in range(7 - (first_weekday + num_days) % 7):
html_content += "<td></td>"
html_content += "</tr></table>"
current_date += relativedelta(months=1)
html_content += """
</body>
</html>
"""
with open('calendar.html', 'w') as f:
f.write(html_content)
print("HTML calendar generated successfully!")
# Example usage
generate_calendar_html('YearList.txt')