Add Python Script for Multiple Rows

This commit is contained in:
thanhtl 2025-02-19 20:52:35 +07:00
commit bf04da2dfe

View File

@ -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!")