今天想試試看加入預算在excel表格裡,在那之前先在輸出提示的地方做修改:
=== 4. 主程式 ===
if name == "main":
city = input("請輸入旅遊城市:")
days = input("請輸入旅遊天數:")
style = input("請輸入旅遊風格(美食/文化/購物/自然/混合):")
user_request = f"設計一個{days}日的{city}旅遊行程,偏好{style},並在每個餐飲或景點後加上預估金額(約XXX元)。"
full_prompt = f"""
根據以下需求設計詳細行程:
{user_request}
請包含:
上午、下午、晚上安排
推薦景點與餐廳
建議交通方式
Google Maps 連結
每項活動加上「約XXX元」的預算
"""
檢查了一下,有這種輸出結果就可以開始重做excel的格式
在原有的格式下參考GPT的建議,新增一格為預算,會遍歷純文字內容,按行處理,從行中擷取費用, 根據內容類型(時間,Google Maps 連結,交通方式)寫入不同的欄位
def save_as_excel(filename, itinerary_text):
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "行程與預算"
headers = ["時間", "活動/景點", "交通方式", "Google Maps", "預估費用 (NTD)"]
ws.append(headers)
for col in range(1, len(headers) + 1):
ws.cell(row=1, column=col).font = Font(bold=True)
ws.cell(row=1, column=col).alignment = Alignment(horizontal="center", vertical="center")
total_budget = 0
for line in itinerary_text.split("\n"):
cost = None
match = re.search(r"(\d{2,5})\s*(元|NTD|塊)", line)
if match:
cost = int(match.group(1))
total_budget += cost
if "上午" in line or "下午" in line or "晚上" in line:
ws.append([line.strip(), "", "", "", ""])
elif "https://maps.google.com" in line:
ws.append(["", "", "", line.strip(), ""])
elif "交通" in line:
ws.append(["", "", line.strip(), "", ""])
else:
if line.strip():
ws.append(["", line.strip(), "", "", cost if cost else ""])
ws.append([])
ws.append(["", "", "", "總預算", total_budget])
ws.cell(row=ws.max_row, column=4).font = Font(bold=True)
ws.cell(row=ws.max_row, column=5).font = Font(bold=True)
for col in "ABCDE":
ws.column_dimensions[col].width = 20
wb.save(filename)
修改完成明天來試用看看吧