Advertisement
Python253

csv2pdf

Mar 13th, 2024
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: csv2pdf.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a CSV file (.csv) to a PDF file (.pdf).
  10. It reads the CSV content and generates a PDF with each row in a separate cell.
  11.  
  12. Requirements:
  13. - Python 3.x
  14. - FPDF library (install using: pip install fpdf)
  15.  
  16. Usage:
  17. 1. Save this script as 'csv2pdf.py'.
  18. 2. Ensure your CSV file ('example.csv') is in the same directory as the script.
  19. 3. Install the FPDF library using the command: 'pip install fpdf'
  20. 4. Open a terminal and navigate to the directory containing the script.
  21. 5. Run the script.
  22. 6. The converted PDF file ('csv2pdf.pdf') will be generated in the same directory.
  23.  
  24. Note: Adjust the 'csv_filename' and 'pdf_filename' variables in the script as needed.
  25. """
  26.  
  27. from fpdf import FPDF
  28. import csv
  29.  
  30. def csv_to_pdf(csv_filename, pdf_filename):
  31.  pdf = FPDF()
  32.  pdf.add_page()
  33.  pdf.set_font("Arial", size=12)
  34.  
  35.  with open(csv_filename, 'r', newline='') as csvfile:
  36.      csvreader = csv.reader(csvfile)
  37.      for row in csvreader:
  38.          pdf.cell(0, 10, ', '.join(row), ln=True)
  39.  
  40.  pdf.output(pdf_filename)
  41.  
  42. if __name__ == "__main__":
  43.  csv_filename = 'example.csv'
  44.  pdf_filename = 'csv2pdf.pdf'
  45.  csv_to_pdf(csv_filename, pdf_filename)
  46.  print(f"Converted '{csv_filename}' to '{pdf_filename}'.")
  47.  
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement