Use direct Python execution for reliable document creation including spreadsheets, PDFs, and structured reports
Use direct run_shell with Python scripts for document operations when:
openpyxl, pandas, reportlab, FPDF, or similar librariesThe shell_agent tool can:
Direct run_shell with Python is more reliable because it:
.py files first avoids shell_agent parsing issues with heredocsWhen web research tools (search_web, read_webpage) fail or return 'unknown error':
run_shell pattern to generate documents even without web-sourced content.Example contingency workflow for pharmacy compliance:
# When web research fails, proceed with established regulatory knowledge
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
checklist_items = [
"Verify pharmacist license is current and displayed",
"Maintain controlled substance inventory logs",
"Ensure proper storage temperatures for medications",
"Keep patient counseling records for controlled substances",
"Display required pharmacy signage and notices"
]
# Generate PDF using these standard compliance items
This approach was successfully used in task 045aba2e-4093-42aa-ab7f-159cc538278c_phase2 where all web tools failed but pharmacy compliance PDFs were still created successfully.
For complex multi-line scripts, especially when using shell_agent as executor:
# Step 1: Write the Python script to a file
cat > generate_document.py << 'EOF'
import openpyxl
from openpyxl import Workbook
# Your document code here
wb = openpyxl.load_workbook('file.xlsx')
# ... operations ...
wb.save('output.xlsx')
print('Success')
EOF
# Step 2: Execute the script
python3 generate_document.py
For short, simple scripts when NOT using shell_agent as the executor:
python3 << 'EOF'
import pandas as pd
df = pd.read_excel('input.xlsx')
df.to_excel('output.xlsx', index=False)
print('Done')
EOF
Write to file first, then execute:
import pandas as pd
# Load data from specific sheet
df = pd.read_excel('input.xlsx', sheet_name='Revenue')
# Apply transformations
df['Net_Revenue'] = df['Gross_Revenue'] * (1 - df['Tax_Rate'])
# Save results
df.to_excel('output.xlsx', index=False, sheet_name='Processed')
from openpyxl import load_workbook
wb = load_workbook('tour_data.xlsx')
# Iterate through sheets
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
# Apply formatting or calculations
for row in ws.iter_rows(min_row=2, max_col=5):
# Process cells
pass
wb.save('tour_data_processed.xlsx')
Write to file first, then execute:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
def create_checklist(filename, items):
c = canvas.Canvas(filename, pagesize=letter)
width, height = letter
# Title
c.setFont("Helvetica-Bold", 20)
c.drawString(1*inch, height - 1*inch, "Task Checklist")
# Items
c.setFont("Helvetica", 14)
y_position = height - 1.5*inch
for i, item in enumerate(items, 1):
checkbox = "☐" # Empty checkbox
c.drawString(1*inch, y_position, f"{checkbox} {item}")
y_position -= 0.3*inch
c.save()
print(f"Created {filename} with {len(items)} items")
# Usage
items = ["Review requirements", "Complete analysis", "Submit report", "Follow up"]
create_checklist("checklist.pdf", items)
from fpdf import FPDF
class PDF(FPDF):
def header(self):
self.set_font('Arial', 'B', 15)
self.cell(0, 10, 'Monthly Report', 0, 1, 'C')
self.ln(10)
def footer(self):
self.set_y(-15)
self.set_font('Arial', 'I', 8)
self.cell(0, 10, f'Page {self.page_no()}', 0, 0, 'C')
def create_report(filename, data):
pdf = PDF()
pdf.add_page()
pdf.set_font('Arial', '', 12)
# Add content
for section, content in data.items():
pdf.set_font('Arial', 'B', 12)
pdf.cell(0, 10, section, 0, 1)
pdf.set_font('Arial', '', 12)
pdf.multi_cell(0, 8, content)
pdf.ln(5)
pdf.output(filename)
print(f"Report saved to {filename}")
# Usage
data = {
"Executive Summary": "This report covers Q4 performance metrics.",
"Key Findings": "Revenue increased by 15% compared to Q3.",
"Recommendations": "Continue current strategy with minor adjustments."
}
create_report("report.pdf", data)
import pandas as pd
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.lib import colors
# Step 1: Process spreadsheet data
df = pd.read_excel('sales_data.xlsx')
summary = df.groupby('Region')['Revenue'].sum().reset_index()
# Step 2: Generate PDF report
doc = SimpleDocTemplate("sales_report.pdf", pagesize=letter)
elements = []
# Create table from data
data = [['Region', 'Revenue']]
for _, row in summary.iterrows():
data.append([row['Region'], f"${row['Revenue']:,.2f}"])
table = Table(data)
table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('GRID', (0, 0), (-1, -1), 1, colors.black),
]))
elements.append(table)
doc.build(elements)
print("PDF report generated successfully")
Write to file first, then execute:
import sys
from openpyxl import load_workbook