@@ -211,6 +211,7 @@ def create_excel_report(stock_analysis):
211211 row_headers = [
212212 "Stock Code",
213213 f"{datetime.now().date()} Stock Price",
214+ "Previous Day Stock Price",
214215 "Compared to day before",
215216 "Nikkei Perception",
216217 "Yahoo Finance Perception",
@@ -223,10 +224,18 @@ def create_excel_report(stock_analysis):
223224 cell.alignment = Alignment(horizontal='left', vertical='center')
224225 ws.row_dimensions[row].height = 20
225226
227+ def color_sentiment(cell, sentiment):
228+ if sentiment == "Positive" or sentiment == "Very Positive":
229+ cell.fill = PatternFill(start_color="00FF00", end_color="00FF00", fill_type="solid")
230+ elif sentiment == "Negative" or sentiment == "Very Negative":
231+ cell.fill = PatternFill(start_color="FF0000", end_color="FF0000", fill_type="solid")
232+ elif sentiment == "Neutral":
233+ cell.fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
234+
226235 # Populate data
227236 for col, stock in enumerate(stock_analysis, start=2):
228237 current_price = stock['current_stock_price']
229- previous_price = stock['stock_price_data'][1][1] if len(stock['stock_price_data']) > 1 else current_price
238+ previous_price = stock['stock_price_data'][1][1] if len(stock['stock_price_data']) > 1 else None
230239
231240 if current_price is not None and previous_price is not None:
232241 price_change_percent = ((current_price - previous_price) / previous_price) * 100
@@ -235,8 +244,9 @@ def create_excel_report(stock_analysis):
235244
236245 ws.cell(row=2, column=col, value=stock['stock_number'])
237246 ws.cell(row=3, column=col, value=current_price if current_price is not None else "N/A")
247+ ws.cell(row=4, column=col, value=previous_price if previous_price is not None else "N/A")
238248
239- change_cell = ws.cell(row=4 , column=col, value=f"{price_change_percent:.2f}%" if price_change_percent != "N/A" else price_change_percent)
249+ change_cell = ws.cell(row=5 , column=col, value=f"{price_change_percent:.2f}%" if price_change_percent != "N/A" else price_change_percent)
240250 if price_change_percent != "N/A":
241251 if price_change_percent > 0:
242252 change_cell.fill = PatternFill(start_color="00FF00", end_color="00FF00", fill_type="solid")
@@ -245,21 +255,23 @@ def create_excel_report(stock_analysis):
245255 else:
246256 change_cell.fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
247257
248- ws.cell(row=5, column=col, value=stock['nikkei_sentiment'])
249- ws.cell(row=6, column=col, value=stock['yahoo_sentiment'])
250- ws.cell(row=7, column=col, value=stock['overall_sentiment'])
258+ nikkei_cell = ws.cell(row=6, column=col, value=stock['nikkei_sentiment'])
259+ color_sentiment(nikkei_cell, stock['nikkei_sentiment'])
260+
261+ yahoo_cell = ws.cell(row=7, column=col, value=stock['yahoo_sentiment'])
262+ color_sentiment(yahoo_cell, stock['yahoo_sentiment'])
263+
264+ overall_cell = ws.cell(row=8, column=col, value=stock['overall_sentiment'])
265+ color_sentiment(overall_cell, stock['overall_sentiment'])
251266
252- action_cell = ws.cell(row=8 , column=col, value=stock['action_recommendation'].split('\n')[0]) # Only the action, not the explanation
267+ action_cell = ws.cell(row=9 , column=col, value=stock['action_recommendation'].split('\n')[0]) # Only the action, not the explanation
253268 if "Buy" in action_cell.value:
254269 action_cell.fill = PatternFill(start_color="00FF00", end_color="00FF00", fill_type="solid")
255270
256271 # Save the workbook
257272 filename = f"stock_analysis_{datetime.now().strftime('%Y%m%d')}.xlsx"
258273 wb.save(filename)
259274 print(f"Excel report saved as {filename}")
260- # Save the workbook
261- filename = f"stock_analysis_{datetime.now().strftime('%Y%m%d')}.xlsx"
262- wb.save(filename)
263275
264276def main():
265277 stock_data = get_stock_codes_and_names()
0 commit comments