Python kodunu PDF'ye çevirmek için birkaç farklı yöntem kullanılabilir:
- ReportLab: Bu kütüphane, Python ile PDF oluşturma imkanı sunar ve kodun tasarımını özelleştirmeye olanak tanır 1.
from reportlab.pdfgen import canvas inp = 'example.py' out = 'output.pdf' pdf = canvas.Canvas(out) with open(inp, 'r') as f: pdf.drawString(100, 800, f.read()) pdf.save()
- FPDF: Metin tabanlı içerikten PDF oluşturmak için kullanılan hafif bir kütüphanedir 1.
from fpdf import FPDF inp = 'example.py' out = 'output.pdf' pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) with open(inp, 'r') as f: txt = f.read() pdf.multi_cell(0, 10, txt) pdf.output(out)
- Matplotlib: Bu kütüphane, metinleri içeren bir figür oluşturup bunu PDF olarak kaydederek görsel bir sunum sağlar 1.
import matplotlib.pyplot as plt inp = 'example.py' out = 'output.pdf' with open(inp, 'r') as f: txt = f.read() fig, ax = plt.subplots() ax.text(0.1, 0.5, txt, wrap=True, fontsize=12) ax.axis('off') plt.savefig(out, format='pdf')
import pdfkit html_file = 'example.html' output_pdf = 'output.pdf' pdfkit.from_file(html_file, output_pdf)
5 kaynaktan alınan bilgiyle göre: