Resume Builder nano app text convert to pdf | SAMRAT TAYADE
tkinter
and fpdf
libraries installed in your Python environment for the code to work properly. You can install them using pip install tkinter
and pip install fpdf
.import tkinter as tk
from tkinter import filedialog
from fpdf import FPDF
class TextToPDFConverter:
def __init__(self, root):
self.root = root
self.root.title("Text to PDF Converter")
self.name_label = tk.Label(self.root, text="Name:")
self.name_label.pack(pady=5)
self.name_entry = tk.Entry(self.root)
self.name_entry.pack(pady=5)
self.address_label = tk.Label(self.root, text="Address:")
self.address_label.pack(pady=5)
self.address_entry = tk.Entry(self.root)
self.address_entry.pack(pady=5)
self.qualification_label = tk.Label(self.root, text="Qualification:")
self.qualification_label.pack(pady=5)
self.qualification_entry = tk.Entry(self.root)
self.qualification_entry.pack(pady=5)
self.text_label = tk.Label(self.root, text="Enter More:")
self.text_label.pack(pady=5)
self.text_entry = tk.Text(self.root, height=10, width=50)
self.text_entry.pack(pady=5)
self.convert_button = tk.Button(self.root, text="Convert to PDF", command=self.convert_to_pdf)
self.convert_button.pack(pady=5)
def convert_to_pdf(self):
name = self.name_entry.get()
address = self.address_entry.get()
qualification = self.qualification_entry.get()
text = self.text_entry.get("1.0", tk.END)
# Create a PDF object
pdf = FPDF()
pdf.add_page()
# Set the font and font size
pdf.set_font("Arial", size=12)
# Write the name, address, and qualification to the PDF
pdf.cell(0, 10, txt="Name: " + name, ln=True)
pdf.cell(0, 10, txt="Address: " + address, ln=True)
pdf.cell(0, 10, txt="Qualification: " + qualification, ln=True)
pdf.ln()
# Write the text to the PDF
lines = text.split("\n")
for line in lines:
pdf.multi_cell(0, 10, txt=line)
# Choose a file location for saving the PDF
file_path = filedialog.asksaveasfilename(defaultextension=".pdf")
if file_path:
# Save the PDF
pdf.output(file_path)
# Show a success message
tk.messagebox.showinfo("Success", "PDF saved successfully!")
else:
# Show an error message
tk.messagebox.showerror("Error", "Please choose a valid file path.")
root = tk.Tk()
app = TextToPDFConverter(root)
root.mainloop()
0 Comments