Spaces:
Sleeping
Sleeping
File size: 13,249 Bytes
8c1f582 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
#!/usr/bin/env python3
"""
Simple PDF Report Generator using reportlab
Generates professional PDF reports from UN motion analysis results without
requiring heavy dependencies like weasyprint.
Usage:
python scripts/generate_simple_pdf.py <input_file> [--output output.pdf]
Example:
python scripts/generate_simple_pdf.py analysis/01_gaza_ceasefire_resolution_israel_bilateral_impact_latest.json
"""
import argparse
import json
import sys
from datetime import datetime
from pathlib import Path
from typing import Optional, List, Dict
# Add project root to path
PROJECT_ROOT = Path(__file__).parent.parent
sys.path.insert(0, str(PROJECT_ROOT))
def check_dependencies():
"""Check if required dependencies are installed"""
try:
from reportlab.lib.pagesizes import letter, A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib import colors
return True
except ImportError as e:
print(f"Missing dependency: reportlab")
print("\nInstall with:")
print(" pip install reportlab")
return False
def generate_bilateral_impact_pdf(json_file: Path, output_pdf: Optional[Path] = None):
"""
Generate PDF report from bilateral impact analysis JSON
Args:
json_file: Path to JSON analysis results
output_pdf: Optional output PDF path
"""
from reportlab.lib.pagesizes import letter, A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_JUSTIFY
# Load JSON data
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Determine output path
if output_pdf is None:
output_pdf = json_file.parent / "pdf" / f"{json_file.stem}.pdf"
# Create output directory
output_pdf.parent.mkdir(parents=True, exist_ok=True)
# Create PDF
doc = SimpleDocTemplate(
str(output_pdf),
pagesize=A4,
rightMargin=72,
leftMargin=72,
topMargin=72,
bottomMargin=18,
)
# Container for the 'Flowable' objects
elements = []
# Define styles
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(
name='CustomTitle',
parent=styles['Heading1'],
fontSize=24,
textColor=colors.HexColor('#1a5490'),
spaceAfter=30,
alignment=TA_CENTER,
fontName='Helvetica-Bold'
))
styles.add(ParagraphStyle(
name='CustomHeading',
parent=styles['Heading2'],
fontSize=16,
textColor=colors.HexColor('#2c5f8d'),
spaceAfter=12,
spaceBefore=12,
fontName='Helvetica-Bold'
))
styles.add(ParagraphStyle(
name='CustomSubHeading',
parent=styles['Heading3'],
fontSize=12,
textColor=colors.HexColor('#34495e'),
spaceAfter=6,
spaceBefore=6,
fontName='Helvetica-Bold'
))
styles.add(ParagraphStyle(
name='Justified',
parent=styles['Normal'],
alignment=TA_JUSTIFY,
fontSize=10,
leading=14
))
# Title
title = Paragraph("Israel Bilateral Relationship Impact Analysis", styles['CustomTitle'])
elements.append(title)
elements.append(Spacer(1, 0.2*inch))
# Metadata
metadata = [
f"<b>Motion:</b> {data['motion_id']}",
f"<b>Analysis Date:</b> {data['timestamp']}",
f"<b>Model:</b> {data['model']}",
f"<b>Countries Analyzed:</b> {data['total_analyzed']}"
]
for line in metadata:
elements.append(Paragraph(line, styles['Normal']))
elements.append(Spacer(1, 0.3*inch))
# Executive Summary
elements.append(Paragraph("Executive Summary", styles['CustomHeading']))
summary_text = f"This report analyzes how the Gaza ceasefire resolution vote affects Israel's bilateral relationships with {data['total_analyzed']} UN member states."
elements.append(Paragraph(summary_text, styles['Justified']))
elements.append(Spacer(1, 0.2*inch))
# Impact Distribution Table
elements.append(Paragraph("Impact Distribution", styles['CustomSubHeading']))
impact_data = [['Impact Category', 'Count', 'Percentage']]
total = data['total_analyzed']
for category, count in data['impact_summary'].items():
pct = (count / total * 100) if total > 0 else 0
impact_data.append([
category.replace('_', ' ').title(),
str(count),
f"{pct:.1f}%"
])
impact_table = Table(impact_data, colWidths=[3.5*inch, 1*inch, 1*inch])
impact_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#1a5490')),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('ALIGN', (1, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 11),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('GRID', (0, 0), (-1, -1), 1, colors.grey),
('ROWBACKGROUNDS', (0, 1), (-1, -1), [colors.white, colors.HexColor('#f9f9f9')]),
]))
elements.append(impact_table)
elements.append(Spacer(1, 0.3*inch))
# Detailed Analyses by Category
elements.append(PageBreak())
elements.append(Paragraph("Detailed Country Analyses", styles['CustomHeading']))
elements.append(Spacer(1, 0.2*inch))
# Group analyses by impact category
by_category = {}
for analysis in data['analyses']:
category = analysis['impact_analysis']['impact_category']
if category not in by_category:
by_category[category] = []
by_category[category].append(analysis)
# Category order
category_order = [
'strengthened_significantly',
'strengthened_moderately',
'strengthened_slightly',
'neutral',
'strained_slightly',
'strained_moderately',
'strained_significantly'
]
for category in category_order:
if category not in by_category or not by_category[category]:
continue
# Category heading
elements.append(Paragraph(
category.replace('_', ' ').title(),
styles['CustomHeading']
))
elements.append(Spacer(1, 0.1*inch))
for i, analysis in enumerate(by_category[category]):
# Country name
elements.append(Paragraph(
f"<b>{analysis['country']}</b>",
styles['CustomSubHeading']
))
# Vote and confidence
info_text = f"<b>Vote:</b> {analysis['vote'].upper()} | <b>Confidence:</b> {analysis['impact_analysis']['confidence']}"
elements.append(Paragraph(info_text, styles['Normal']))
elements.append(Spacer(1, 0.1*inch))
# Analysis reasoning
elements.append(Paragraph("<b>Analysis:</b>", styles['Normal']))
reasoning = analysis['impact_analysis']['reasoning']
elements.append(Paragraph(reasoning, styles['Justified']))
elements.append(Spacer(1, 0.1*inch))
# Key factors
elements.append(Paragraph("<b>Key Factors:</b>", styles['Normal']))
for factor in analysis['impact_analysis']['key_factors']:
elements.append(Paragraph(f"• {factor}", styles['Normal']))
elements.append(Spacer(1, 0.1*inch))
# Country statement
elements.append(Paragraph("<b>Country Statement:</b>", styles['Normal']))
statement = analysis['statement']
if len(statement) > 500:
statement = statement[:500] + "..."
elements.append(Paragraph(statement, styles['Justified']))
# Separator between countries
if i < len(by_category[category]) - 1:
elements.append(Spacer(1, 0.2*inch))
# Space between categories
elements.append(Spacer(1, 0.3*inch))
# Footer
footer_text = f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
elements.append(Spacer(1, 0.3*inch))
elements.append(Paragraph(footer_text, styles['Normal']))
# Build PDF
print(f"Generating PDF: {output_pdf}")
doc.build(elements)
print(f"✓ PDF generated successfully")
return output_pdf
def generate_markdown_pdf(md_file: Path, output_pdf: Optional[Path] = None):
"""
Generate PDF from markdown file
Args:
md_file: Path to markdown file
output_pdf: Optional output PDF path
"""
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_JUSTIFY
# Read markdown
with open(md_file, 'r', encoding='utf-8') as f:
md_content = f.read()
# Determine output path
if output_pdf is None:
output_pdf = md_file.parent / "pdf" / f"{md_file.stem}.pdf"
# Create output directory
output_pdf.parent.mkdir(parents=True, exist_ok=True)
# Create PDF
doc = SimpleDocTemplate(
str(output_pdf),
pagesize=A4,
rightMargin=72,
leftMargin=72,
topMargin=72,
bottomMargin=18,
)
elements = []
styles = getSampleStyleSheet()
# Add custom styles
styles.add(ParagraphStyle(
name='CustomTitle',
parent=styles['Heading1'],
fontSize=20,
textColor=colors.HexColor('#1a5490'),
spaceAfter=20,
fontName='Helvetica-Bold'
))
styles.add(ParagraphStyle(
name='Justified',
parent=styles['Normal'],
alignment=TA_JUSTIFY,
fontSize=10,
leading=14
))
# Simple markdown parsing (basic headers and paragraphs)
lines = md_content.split('\n')
for line in lines:
line = line.strip()
if not line:
elements.append(Spacer(1, 0.1*inch))
continue
if line.startswith('# '):
elements.append(Paragraph(line[2:], styles['CustomTitle']))
elif line.startswith('## '):
elements.append(Paragraph(line[3:], styles['Heading2']))
elif line.startswith('### '):
elements.append(Paragraph(line[4:], styles['Heading3']))
elif line.startswith('**') or line.startswith('*'):
elements.append(Paragraph(line, styles['Normal']))
elif line.startswith('---'):
elements.append(Spacer(1, 0.2*inch))
else:
elements.append(Paragraph(line, styles['Justified']))
# Build PDF
print(f"Generating PDF: {output_pdf}")
doc.build(elements)
print(f"✓ PDF generated successfully")
return output_pdf
def main():
parser = argparse.ArgumentParser(
description="Generate PDF reports from analysis results (lightweight version)",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Convert JSON bilateral impact to PDF
python scripts/generate_simple_pdf.py tasks/analysis/01_gaza_ceasefire_resolution_israel_bilateral_impact_latest.json
# Convert markdown to PDF
python scripts/generate_simple_pdf.py analysis/report.md
# Specify output path
python scripts/generate_simple_pdf.py analysis/report.md --output custom.pdf
"""
)
parser.add_argument(
"input_file",
type=Path,
help="Input file (.md or .json)"
)
parser.add_argument(
"--output",
type=Path,
help="Output PDF file (optional)"
)
args = parser.parse_args()
# Check dependencies
if not check_dependencies():
sys.exit(1)
# Validate input file
if not args.input_file.exists():
print(f"Error: Input file not found: {args.input_file}")
sys.exit(1)
try:
# Determine file type and process
if args.input_file.suffix == '.json':
print("Processing bilateral impact JSON...")
pdf_path = generate_bilateral_impact_pdf(args.input_file, args.output)
elif args.input_file.suffix == '.md':
print("Processing markdown file...")
pdf_path = generate_markdown_pdf(args.input_file, args.output)
else:
print(f"Error: Unsupported file type: {args.input_file.suffix}")
print("Supported types: .md, .json")
sys.exit(1)
print(f"\n✓ PDF report generated: {pdf_path}")
print(f" Size: {pdf_path.stat().st_size / 1024:.1f} KB")
except Exception as e:
print(f"\n❌ Error generating PDF: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()
|