-
Notifications
You must be signed in to change notification settings - Fork 1
/
report_generation.py
685 lines (577 loc) · 28.3 KB
/
report_generation.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import os
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib
import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
import logging
from config import ( # Importing email configurations
EMAIL_FROM, EMAIL_TO, SMTP_SERVER, SMTP_USERNAME, SMTP_PASSWORD, SMTP_PORT
)
from datetime import timedelta
import openai
import re
import json
from email.mime.application import MIMEApplication
import traceback
import glob
from api_clients import api_call_with_retries
def generate_html_report_with_recommendations(report_entries, digest_summary, gpt_recommendations, plot_image_path='top_coins_plot.png'):
"""
Generates an HTML report with summaries from the report entries, GPT-4o recommendations, and a plot of the top coins.
Args:
report_entries (list): List of report entries to include in the report.
digest_summary (dict): Summary of the Sundown Digest to include at the top.
gpt_recommendations (dict): GPT-4o's recommendations for coin purchases, structured as a list of dictionaries.
plot_image_path (str): Path to the plot image to embed in the report.
Returns:
str: HTML content of the report.
"""
# Sundown Digest Summary section
digest_items = ''.join(f'<li style="font-size:14px;line-height:1.6;">{item}</li>'
for item in digest_summary.get('surge_summary', [])) if digest_summary else ''
tickers = ', '.join(digest_summary.get('tickers', [])) if digest_summary else 'N/A'
digest_html = f"""
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#fff;">
<tr>
<td style="padding:20px;">
<h3 style="font-size:20px;color:#2a9d8f;margin-bottom:10px;">Sundown Digest Summary</h3>
<p style="font-size:14px;line-height:1.6;"><strong>Tickers Mentioned:</strong> {tickers}</p>
<p style="font-size:14px;line-height:1.6;"><strong>News Summary:</strong></p>
<ul style="list-style-type:disc;padding-left:20px;margin:0;">
{digest_items}
</ul>
</td>
</tr>
</table>
"""
# Color Explanation
color_explanation = """
<p style="font-size:14px;line-height:1.6;">
<strong>Color Meaning:</strong><br>
<span style="background-color:#d4edda;padding:2px 5px;border-radius:3px;">Green</span>: Indicates coins expected to surge or break out.<br>
<span style="background-color:#ffe5b4;padding:2px 5px;border-radius:3px;">Orange</span>: Indicates coins not expected to surge.
</p>
"""
# AI Recommendations Section
if not gpt_recommendations or not gpt_recommendations.get('recommendations'):
recommendations_html = """
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#fff;">
<tr>
<td style="padding:20px;">
<h3 style="font-size:20px;color:#2a9d8f;margin-bottom:10px;">AI Generated Coin Recommendations</h3>
<p style="font-size:14px;line-height:1.6;">No coins are currently recommended for purchase based on the analysis.</p>
</td>
</tr>
</table>
"""
plot_html = "" # No plot if no recommendations
else:
recommendation_items = ''
for item in gpt_recommendations['recommendations']:
# Match the coin with report entries to fetch URL, cumulative score percentage
matching_entry = next((entry for entry in report_entries if entry["coin_name"].lower() == item["coin"].lower()), None)
# CoinPaprika URL format or other URL source can be used here
coin_url = f"https://coinpaprika.com/coin/{matching_entry['coin_id']}/" if matching_entry else '#'
cumulative_score_percentage = matching_entry.get('cumulative_score_percentage', 'N/A') if matching_entry else 'N/A'
# Determine background color based on expected surge status
background_color = "#d4edda" if item.get("expected_to_surge", 'Yes') else "#ffe5b4"
# Capitalize each word in the coin name
coin_name = item["coin"].title()
recommendation_items += f"""
<li style="font-size:14px;line-height:1.6;margin-bottom:10px;background-color:{background_color};padding:10px;border-radius:5px;">
<b>{coin_name}</b> - {item["reason"]}<br>
<strong>Cumulative Score Percentage:</strong> {cumulative_score_percentage}%<br>
<a href="{coin_url}" target="_blank" style="color:#0077cc;text-decoration:none;">More Info</a>
</li>
"""
recommendations_html = f"""
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#fff;">
<tr>
<td style="padding:20px;">
<h3 style="font-size:20px;color:#2a9d8f;margin-bottom:10px;">AI Generated Coin Recommendations</h3>
{color_explanation}
<p style="font-size:14px;line-height:1.6;"><strong>Meaning of Cumulative Score Percentage:</strong> a higher percentage indicates a stronger potential based on historical data and analysis.</p>
<ul style="list-style-type:disc;padding-left:20px;margin:0;">
{recommendation_items}
</ul>
</td>
</tr>
</table>
"""
# Embed the attached image in the HTML using CID
cid = "top_coins_plot" # This should match the Content-ID of the attached image
plot_html = f"""
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#fff;">
<tr>
<td style="padding:20px;text-align:center;">
<h3 style="font-size:20px;color:#2a9d8f;margin-bottom:10px;">Top Coins Cumulative Scores Over Time</h3>
<img src="cid:{cid}" alt="Top Coins Plot" style="width:100%;max-width:600px;height:auto;"/>
</td>
</tr>
</table>
"""
# Full HTML structure
html_content = f"""
<html>
<body style="margin:0;padding:0;background-color:#f9f9f9;font-family:Arial,sans-serif;color:#333;">
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#f9f9f9;">
<tr>
<td align="center">
<table width="600" cellpadding="0" cellspacing="0" border="0" style="background-color:#fff;">
<tr>
<td style="padding:20px;">
<h2 style="text-align:center;color:#264653;font-size:24px;margin:0;">Coin Analysis Report</h2>
</td>
</tr>
<tr>
<td>
{digest_html}
</td>
</tr>
<tr>
<td>
{recommendations_html}
</td>
</tr>
{plot_html}
<tr>
<td style="padding:20px;">
<p style="text-align:center;color:#777;font-size:12px;margin:0;">Report generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
"""
return html_content
def gpt4o_analyze_and_recommend(df):
"""
Uses GPT-4o to analyze the final results DataFrame and provide structured recommendations for coin purchases.
Parameters:
df (pd.DataFrame): The final DataFrame containing coin analysis results.
Returns:
dict: A structured summary of GPT-4o's recommendations for coin purchases, including reasons.
"""
# Convert DataFrame to JSON for input
df_json = df.to_dict(orient='records')
# Prepare prompt
prompt = f"""
You are provided with detailed analysis data for several cryptocurrency coins. Using this data, evaluate each coin individually and provide a recommendation on whether it should be considered for purchase based on the potential for a breakout or surge in value.
**Key requirements:**
1. If the analysis indicates a surge or breakout potential for a coin, the recommendation must always be "Yes". Ensure this is clearly stated and supported by the data.
2. If a coin does not show immediate potential, include it in the output with a detailed explanation, and the recommendation must be "No". Ensure the reasoning reflects the lack of a surge or breakout potential and is grounded in the data.
**Do not repeat or summarize the dataset.** Instead, return the recommendations in structured JSON format for each coin, whether recommended for purchase or not, with clear reasoning for your recommendation based on the data.
Ensure the explanation explicitly references key factors from the data, such as liquidity risk, cumulative score, sentiment, or other relevant metrics, to justify the recommendation.
Format your response as follows:
{{
"recommendations": [
{{
"coin": "Coin Name",
"liquidity_risk": "Low/Medium/High",
"cumulative_score": "Score Value",
"recommendation": "Yes/No",
"reason": "Provide a fluent, specific, and data-driven reason based on the analysis provided. Clearly explain why this coin is or is not recommended for purchase, citing relevant metrics or trends from the data."
}},
...
]
}}
Here is the data for your analysis:
{json.dumps(df_json, indent=2)}
"""
def api_call():
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
n=1,
stop=None,
temperature=0.0
)
return response
try:
# Call the API with retries
response = api_call_with_retries(api_call)
gpt_message_content = response['choices'][0]['message']['content']
# Extract JSON content from the response
json_match = re.search(r'```json(.*?)```', gpt_message_content, re.DOTALL)
if json_match:
json_content = json_match.group(1).strip()
parsed_data = json.loads(json_content)
logging.debug(f"Parsed JSON data: {parsed_data}")
return parsed_data
logging.debug("No JSON content found in the GPT response.")
return {"recommendations": []}
except Exception as e:
logging.error(f"Failed to complete GPT-4o analysis: {e}")
return {"recommendations": []}
def send_failure_email():
"""
Sends an email with the current results when the script encounters an error.
If no flag file for the current date exists, it deletes all previous flag files,
sends the email, and creates a flag file for today.
"""
# Get today's date in the format YYYY-MM-DD
today = datetime.now().strftime("%Y-%m-%d")
# Define the flag file path for today's date
flag_file = f"email_sent_{today}.flag"
# Check if the flag file for today already exists
if os.path.exists(flag_file):
logging.debug(f"Email already sent today ({today}). Skipping email.")
return # Exit the function if email has already been sent today
# Delete all previous flag files if today's flag file does not exist
flag_files = glob.glob("email_sent_*.flag")
for file in flag_files:
try:
os.remove(file)
logging.debug(f"Deleted old flag file: {file}")
except Exception as e:
logging.debug(f"Failed to delete flag file {file}: {e}")
# Proceed to send the email if no flag file exists for today
if os.path.exists(RESULTS_FILE):
with open(RESULTS_FILE, 'r') as file:
file_contents = file.read()
else:
file_contents = "No data available, as the results file was not created."
# HTML content with inline CSS for the failure email
html_content = f"""
<html>
<head>
<style>
body {{
font-family: Arial, sans-serif;
color: #333;
}}
h2 {{
color: #c0392b;
}}
p {{
font-size: 14px;
color: #555;
}}
.content {{
background-color: #f9f9f9;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
}}
.content pre {{
background-color: #f4f4f4;
border: 1px solid #ccc;
padding: 10px;
border-radius: 3px;
}}
</style>
</head>
<body>
<h2>Failure in Weekly Coin Analysis Script</h2>
<p>The script encountered an error. Below are the current results:</p>
<div class="content">
<pre>{file_contents}</pre>
</div>
</body>
</html>
"""
msg = MIMEMultipart('alternative')
msg['Subject'] = "Failure in Weekly Coin Analysis Script"
msg['From'] = EMAIL_FROM
#msg['To'] = EMAIL_TO
msg['Bcc'] = EMAIL_TO # Add BCC field (replace EMAIL_BCC with your BCC email address)
part = MIMEText(html_content, 'html')
msg.attach(part)
try:
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SMTP_USERNAME, SMTP_PASSWORD)
recipients = EMAIL_TO.split(",") # Add BCC recipients to the send list
server.sendmail(EMAIL_FROM, recipients, msg.as_string())
logging.debug("Failure email sent successfully.")
# Create today's flag file to avoid sending multiple emails
with open(flag_file, 'w') as f:
f.write("Email sent")
except Exception as e:
logging.debug(f"Failed to send email: {e}")
def print_command_line_report(report_entries):
"""
Prints a command-line report of the daily coin analysis.
Parameters
----------
report_entries : list
A list of dictionaries, each containing the analysis results for a single coin.
Returns
-------
None
"""
df = pd.DataFrame(report_entries)
logging.debug("\nCoin Analysis Report")
logging.debug(tabulate(df, headers="keys", tablefmt="grid"))
logging.debug(f"\nReport generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
def save_report_to_excel(report_entries, filename='coin_analysis_report.xlsx'):
"""
Saves the report entries to an Excel file with enhanced formatting and styling.
Args:
report_entries (list): A list of dictionaries containing the report data.
filename (str): The name of the Excel file to save the report to.
"""
# Convert the report entries to a pandas DataFrame
df = pd.DataFrame(report_entries)
# Save DataFrame to an Excel file without formatting
df.to_excel(filename, index=False)
# Open the Excel file with openpyxl for formatting
workbook = load_workbook(filename)
sheet = workbook.active
# Define styles for headers and cells
header_font = Font(bold=True, color="FFFFFF", size=11)
header_fill = PatternFill("solid", fgColor="4F81BD")
cell_font = Font(name="Arial", size=10)
cell_alignment = Alignment(horizontal="left", vertical="top", wrap_text=False) # Turn off wrap_text for content cells
# Define border style
thin_border = Border(left=Side(style="thin"), right=Side(style="thin"),
top=Side(style="thin"), bottom=Side(style="thin"))
# Apply header styles (background color, font, alignment)
for col in sheet.iter_cols(min_row=1, max_row=1, min_col=1, max_col=sheet.max_column):
max_length = 0
column = col[0].column_letter # Get the column letter for header
for cell in col:
cell.font = header_font
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=False) # Turn wrapping off for headers
cell.border = thin_border
# Adjust column width based on header content
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
adjusted_width = (max_length + 2) * 1.2 # Add some padding for headers
sheet.column_dimensions[column].width = adjusted_width
# Apply cell styles (font, alignment, borders) and auto-adjust column width based on content
for col in sheet.iter_cols(min_row=1, max_row=sheet.max_row, min_col=1, max_col=sheet.max_column):
max_length = 0
column = col[0].column_letter # Get the column letter for data cells
for cell in col:
cell.font = cell_font
cell.alignment = cell_alignment
cell.border = thin_border
# Adjust column width based on the content
try:
if cell.value:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except Exception as e:
print(f"Error processing cell {cell.coordinate}: {e}")
# Set the column width to fit the content with padding
adjusted_width = (max_length + 2) * 1.2 # Add padding for cells
sheet.column_dimensions[column].width = adjusted_width
# Freeze the top row (headers) for better readability
sheet.freeze_panes = "A2"
# Save the workbook with the formatting applied
try:
workbook.save(filename)
print(f"Report saved to {filename} with enhanced formatting.")
except Exception as e:
print(f"Error saving the report: {e}")
finally:
workbook.close()
return filename
def gpt4o_summarize_digest_and_extract_tickers(digest_text):
"""
Uses GPT-4 to summarize the Sundown Digest and extract key points related to potential surges in coin value.
Args:
digest_text (str): The concatenated text from all digest entries.
Returns:
dict: A dictionary containing a summary focused on surge-causing news and a list of extracted tickers.
"""
prompt = f"""
Analyze the following digest entries and provide the following:
1) A concise summary in bullet points (no more than 250 words) of key news items likely to cause surges in the value of the mentioned coins.
2) List the relevant cryptocurrency tickers beside each news item. Ensure there is no duplication.
Text:
{digest_text}
Respond **only** in JSON format with 'surge_summary' and 'tickers' as keys. Ensure the tickers are in alphabetical order and there are no duplicate tickers.
"""
try:
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
n=1,
stop=None,
temperature=0.0
)
# Extract the content of the response
response_content = response.choices[0].message['content'].strip()
# Use a regular expression to extract JSON from the response content
json_match = re.search(r'\{.*\}', response_content, re.DOTALL)
if json_match:
json_str = json_match.group(0)
try:
analysis = json.loads(json_str)
return analysis
except json.JSONDecodeError:
logging.debug(f"Failed to decode JSON: {json_str}")
return {"surge_summary": "", "tickers": []}
else:
logging.debug(f"No JSON found in the response: {response_content}")
return {"surge_summary": "", "tickers": []}
except openai.error.RateLimitError as e:
logging.debug(f"Rate limit reached: {e}. Waiting for 60 seconds before retrying...")
time.sleep(60) # Wait before retrying
return gpt4o_summarize_digest_and_extract_tickers(digest_text) # Retry the request
except Exception as e:
logging.debug(f"An error occurred while summarizing the digest and extracting tickers: {e}")
return {"surge_summary": "", "tickers": []}
def summarize_sundown_digest(digest):
"""
Summarizes the Sundown Digest content from the last three days, including sentiment detection,
coin ticker extraction, and a summary focused on news likely to cause surges in coin value.
Args:
digest (list): List of Sundown Digest entries.
Returns:
dict: A dictionary containing key points of news that may cause surges and relevant tickers.
"""
# Get the current date and calculate the date three days ago
current_date = datetime.now()
three_days_ago = current_date - timedelta(days=3)
digest_texts = []
for entry in digest:
# Parse the entry's date
entry_date = datetime.strptime(entry['date'], '%Y-%m-%dT%H:%M:%S.%fZ')
# Filter out entries older than three days
if entry_date < three_days_ago:
continue
digest_texts.append(entry['text'])
# Concatenate all digest texts into a single string
combined_digest_text = " ".join(digest_texts)
# Use GPT-4 to analyze and summarize the combined digest text
summary_and_tickers = gpt4o_summarize_digest_and_extract_tickers(combined_digest_text)
return summary_and_tickers
def send_email_with_report(html_content, attachment_path, plot_image_path='top_coins_plot.png', recommendations=None):
"""
Sends an email with an HTML report and an attached image.
The email uses a 'related' MIME type to allow both HTML and images to be attached.
The HTML content is passed as a string and the image is attached as an inline
attachment with a Content-ID header that matches the CID in the HTML content.
Args:
html_content (str): The HTML content of the email.
attachment_path (str): The path to the Excel file attachment.
plot_image_path (str): The path to the image file to attach.
recommendations (list): List of recommended coins, if any.
Returns:
None
"""
try:
logging.debug(f"Preparing email with attachment: {attachment_path} and plot: {plot_image_path}")
# Create a 'related' multipart message for HTML + images
msg = MIMEMultipart('related') # 'related' allows attaching both HTML and images
msg['Subject'] = "AI Generated Coin Analysis Report"
msg['From'] = EMAIL_FROM
#msg['To'] = EMAIL_TO
msg['Bcc'] = EMAIL_TO # Add BCC field (replace EMAIL_BCC with your BCC email address)
# Attach HTML content
part = MIMEText(html_content, 'html')
msg.attach(part)
# Conditionally attach the plot only if there are recommendations
if recommendations and len(recommendations) > 0:
logging.debug(f"Attaching plot image: {plot_image_path}")
try:
with open(plot_image_path, 'rb') as img_file:
mime_image = MIMEImage(img_file.read(), _subtype='png')
mime_image.add_header('Content-ID', '<top_coins_plot>') # Content-ID should match CID in HTML
mime_image.add_header('Content-Disposition', 'inline', filename="top_coins_plot.png")
msg.attach(mime_image)
except Exception as e:
logging.error(f"Error attaching plot image: {e}")
logging.debug(traceback.format_exc()) # Log the full stack trace
# Attach the Excel file
if os.path.exists(attachment_path):
logging.debug(f"Attaching Excel file: {attachment_path}")
try:
with open(attachment_path, 'rb') as file:
part = MIMEApplication(file.read(), _subtype="xlsx")
part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachment_path))
msg.attach(part)
except Exception as e:
logging.error(f"Error attaching Excel file: {e}")
logging.debug(traceback.format_exc()) # Log the full stack trace
# Send the email
try:
logging.debug(f"Connecting to SMTP server: {SMTP_SERVER}")
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SMTP_USERNAME, SMTP_PASSWORD)
recipients = EMAIL_TO.split(",") # Add BCC recipients to the send list
server.sendmail(EMAIL_FROM, recipients, msg.as_string())
logging.debug("Email sent successfully.")
except Exception as e:
logging.error(f"Error sending email: {e}")
logging.debug(traceback.format_exc()) # Log the full stack trace
except Exception as e:
logging.error(f"An error occurred in send_email_with_report: {e}")
logging.debug(traceback.format_exc()) # Log the full stack trace for debugging
def save_report_to_excel(report_entries, filename='coin_analysis_report.xlsx'):
"""
Saves the report entries to an Excel file with enhanced formatting and styling.
Args:
report_entries (list): A list of dictionaries containing the report data.
filename (str): The name of the Excel file to save the report to.
"""
# Convert the report entries to a pandas DataFrame
df = pd.DataFrame(report_entries)
# Save DataFrame to an Excel file without formatting
df.to_excel(filename, index=False)
# Open the Excel file with openpyxl for formatting
workbook = load_workbook(filename)
sheet = workbook.active
# Define styles for headers and cells
header_font = Font(bold=True, color="FFFFFF", size=11)
header_fill = PatternFill("solid", fgColor="4F81BD")
cell_font = Font(name="Arial", size=10)
cell_alignment = Alignment(horizontal="left", vertical="top", wrap_text=False) # Turn off wrap_text for content cells
# Define border style
thin_border = Border(left=Side(style="thin"), right=Side(style="thin"),
top=Side(style="thin"), bottom=Side(style="thin"))
# Apply header styles (background color, font, alignment)
for col in sheet.iter_cols(min_row=1, max_row=1, min_col=1, max_col=sheet.max_column):
max_length = 0
column = col[0].column_letter # Get the column letter for header
for cell in col:
cell.font = header_font
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=False) # Turn wrapping off for headers
cell.border = thin_border
# Adjust column width based on header content
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
adjusted_width = (max_length + 2) * 1.2 # Add some padding for headers
sheet.column_dimensions[column].width = adjusted_width
# Apply cell styles (font, alignment, borders) and auto-adjust column width based on content
for col in sheet.iter_cols(min_row=1, max_row=sheet.max_row, min_col=1, max_col=sheet.max_column):
max_length = 0
column = col[0].column_letter # Get the column letter for data cells
for cell in col:
cell.font = cell_font
cell.alignment = cell_alignment
cell.border = thin_border
# Adjust column width based on the content
try:
if cell.value:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except Exception as e:
print(f"Error processing cell {cell.coordinate}: {e}")
# Set the column width to fit the content with padding
adjusted_width = (max_length + 2) * 1.2 # Add padding for cells
sheet.column_dimensions[column].width = adjusted_width
# Freeze the top row (headers) for better readability
sheet.freeze_panes = "A2"
# Save the workbook with the formatting applied
try:
workbook.save(filename)
print(f"Report saved to {filename} with enhanced formatting.")
except Exception as e:
print(f"Error saving the report: {e}")
finally:
workbook.close()
return filename