commit bf04da2dfee64430ffb035b2cbba43e050f4cd37 Author: thanhtl Date: Wed Feb 19 20:52:35 2025 +0700 Add Python Script for Multiple Rows diff --git a/Python Script for Multiple Rows b/Python Script for Multiple Rows new file mode 100644 index 0000000..bed78e4 --- /dev/null +++ b/Python Script for Multiple Rows @@ -0,0 +1,42 @@ +import requests +import openpyxl + +# Ollama server URL (update based on your setup) +OLLAMA_URL = "http://192.168.40.142:11434/api/generate" + +# Load the Excel file +file_path = "your_excel_file.xlsx" +wb = openpyxl.load_workbook(file_path) +sheet = wb.active # Select the active sheet + +# Define the column for original text and where to save summaries +text_column = "B" # Column containing text to summarize +summary_column = "C" # Column to store summaries +start_row = 2 # Starting row (assuming row 1 contains headers) + +def summarize_text(text): + """Send text to Ollama for summarization""" + payload = { + "model": "mistral", # Change model if needed + "prompt": f"Summarize the following text:\n\n{text}", + "stream": False + } + response = requests.post(OLLAMA_URL, json=payload) + + if response.status_code == 200: + return response.json()["response"] + else: + return "Error in summarization" + +# Loop through all rows and summarize text +for row in range(start_row, sheet.max_row + 1): + cell_value = sheet[f"{text_column}{row}"].value # Get text from column B + + if cell_value: + print(f"Summarizing row {row}...") + summary = summarize_text(cell_value) + sheet[f"{summary_column}{row}"] = summary # Store summary in column C + +# Save updated Excel file +wb.save(file_path) +print("\nAll summaries saved successfully!")