Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: csv2pdf.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a CSV file (.csv) to a PDF file (.pdf).
- It reads the CSV content and generates a PDF with each row in a separate cell.
- Requirements:
- - Python 3.x
- - FPDF library (install using: pip install fpdf)
- Usage:
- 1. Save this script as 'csv2pdf.py'.
- 2. Ensure your CSV file ('example.csv') is in the same directory as the script.
- 3. Install the FPDF library using the command: 'pip install fpdf'
- 4. Open a terminal and navigate to the directory containing the script.
- 5. Run the script.
- 6. The converted PDF file ('csv2pdf.pdf') will be generated in the same directory.
- Note: Adjust the 'csv_filename' and 'pdf_filename' variables in the script as needed.
- """
- from fpdf import FPDF
- import csv
- def csv_to_pdf(csv_filename, pdf_filename):
- pdf = FPDF()
- pdf.add_page()
- pdf.set_font("Arial", size=12)
- with open(csv_filename, 'r', newline='') as csvfile:
- csvreader = csv.reader(csvfile)
- for row in csvreader:
- pdf.cell(0, 10, ', '.join(row), ln=True)
- pdf.output(pdf_filename)
- if __name__ == "__main__":
- csv_filename = 'example.csv'
- pdf_filename = 'csv2pdf.pdf'
- csv_to_pdf(csv_filename, pdf_filename)
- print(f"Converted '{csv_filename}' to '{pdf_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement