Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: json2pdf.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a JSON file (.json) to a PDF file (.pdf).
- It reads the JSON file and generates a PDF with the specified 'text_content'.
- Requirements:
- - Python 3.x
- - FPDF library (install using: pip install fpdf)
- Usage:
- 1. Save this script as 'json2pdf.py'.
- 2. Ensure your JSON file ('example.json') is in the same directory as the script.
- 3. Install the FPDF library using the command: 'pip install fpdf'
- 4. Run the script.
- 5. The converted PDF file ('json2pdf.pdf') will be generated in the same directory.
- Note: Adjust the 'json_filename' and 'pdf_filename' variables in the script as needed.
- """
- from fpdf import FPDF
- import json
- def json_to_pdf(json_filename, pdf_filename):
- pdf = FPDF()
- pdf.add_page()
- pdf.set_font("Arial", size=12)
- with open(json_filename, 'r') as jsonfile:
- data = json.load(jsonfile)
- if "text_content" in data:
- pdf.multi_cell(0, 10, data["text_content"])
- pdf.output(pdf_filename)
- if __name__ == "__main__":
- json_filename = 'example.json'
- pdf_filename = 'json2pdf.pdf'
- json_to_pdf(json_filename, pdf_filename)
- print(f"Converted '{json_filename}' to '{pdf_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement