Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: xml2pdf.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts an XML file (.xml) to a PDF file (.pdf).
- It extracts text content from all elements in the XML and generates a PDF with each text element in a separate cell.
- Requirements:
- - Python 3.x
- - FPDF library (install using: pip install fpdf)
- Usage:
- 1. Save this script as 'xml2pdf.py'.
- 2. Ensure your XML file ('example.xml') 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 ('xml2pdf.pdf') will be generated in the same directory.
- Note: Adjust the 'xml_filename' and 'pdf_filename' variables in the script as needed.
- """
- from fpdf import FPDF
- import xml.etree.ElementTree as ET
- def xml_to_pdf(xml_filename, pdf_filename):
- pdf = FPDF()
- pdf.add_page()
- pdf.set_font("Arial", size=12)
- tree = ET.parse(xml_filename)
- root = tree.getroot()
- for element in root.iter():
- if element.text:
- pdf.multi_cell(0, 10, element.text)
- pdf.output(pdf_filename)
- if __name__ == "__main__":
- # Set the filenames for the XML and PDF files
- xml_filename = 'example.xml'
- pdf_filename = 'xml2pdf.pdf'
- # Convert the XML to a PDF file
- xml_to_pdf(xml_filename, pdf_filename)
- print(f"Converted '{xml_filename}' to '{pdf_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement