Advertisement
Python253

txt2pdf

Mar 15th, 2024
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: txt2pdf.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a text file (.txt) to a PDF file (.pdf).
  10. It handles word wrapping to ensure that each line in the text file is properly displayed within the PDF.
  11.  
  12. Requirements:
  13. - Python 3.x
  14. - ReportLab library (install using: pip install reportlab)
  15.  
  16. Usage:
  17. 1. Save this script as 'txt2pdf.py'.
  18. 2. Ensure your text file ('example.txt') is in the same directory as the script.
  19. 3. Install the ReportLab library using the command: 'pip install reportlab'
  20. 4. Run the script.
  21. 5. The converted PDF file ('txt2pdf.pdf') will be generated in the same directory.
  22.  
  23. Note: Adjust the 'txt_filename' and 'pdf_filename' variables in the script as needed.
  24. """
  25.  
  26. from reportlab.pdfgen import canvas
  27. from reportlab.lib.pagesizes import letter
  28.  
  29. def txt_to_pdf(txt_filename, pdf_filename):
  30.     with open(txt_filename, 'r', encoding='utf-8') as txtfile:
  31.         lines = txtfile.readlines()
  32.  
  33.     pdf = canvas.Canvas(pdf_filename, pagesize=letter)
  34.  
  35.     # Set the font and size
  36.     pdf.setFont("Helvetica", 12)
  37.  
  38.     # Set the initial y-position and line spacing
  39.     y_position = 750
  40.     line_spacing = 12
  41.  
  42.     # Get the PDF width
  43.     pdf_width, pdf_height = letter
  44.  
  45.     # Iterate through lines and add to PDF with word wrapping
  46.     for line in lines:
  47.         if y_position < 50:
  48.             pdf.showPage()
  49.             y_position = pdf_height - 50  # Reset y-position for the new page
  50.  
  51.         words = line.strip().split()
  52.  
  53.         if not words:
  54.             continue  # Skip empty lines
  55.  
  56.         current_line = words[0]
  57.  
  58.         for word in words[1:]:
  59.             if pdf.stringWidth(current_line + " " + word) < pdf_width - 20:
  60.                 current_line += " " + word
  61.             else:
  62.                 pdf.drawString(10, y_position, current_line)
  63.                 y_position -= line_spacing
  64.                 current_line = word
  65.  
  66.         pdf.drawString(10, y_position, current_line)
  67.         y_position -= line_spacing
  68.  
  69.     pdf.save()
  70.  
  71. if __name__ == "__main__":
  72.     # Set the filenames for the text and PDF files
  73.     txt_filename = 'example.txt'
  74.     pdf_filename = 'txt2pdf.pdf'
  75.  
  76.     # Convert the text to a multi-page PDF file using reportlab with improved word wrapping
  77.     txt_to_pdf(txt_filename, pdf_filename)
  78.  
  79.     print(f"Converted '{txt_filename}' to '{pdf_filename}'.")
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement