Python - Simple PDF Generation
Submitted by razormist on Saturday, May 12, 2018 - 23:56.
In this tutorial we will create a Simple PDF Generation Using Python. Python has a design philosophy which emphasizes code readability. Python is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. You can use this to convert the units value. So let's now do the coding.
There you have it we just created a Simple PDF Generation Using Python. I hope that this simple tutorial help you expand your idea about Python programming. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Getting started
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. Importing Modules After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy the code that I provided below and paste it inside the IDLE text editor- from tkinter import *
- from reportlab.pdfgen import canvas
Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.- root = Tk()
- root.title("Python - Simple PDF Generator")
- width = 700
- height = 400
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- root.geometry("%dx%d+%d+%d" % (width, height, x, y))
- root.resizable(0, 0)
Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.- #========================================FRAME==================================
- Top = Frame(root, width=700, bd=1, relief=SOLID)
- Top.pack(side=TOP)
- Mid = Frame(root, width=700, pady=30)
- Mid.pack(side=TOP)
- Content = Frame(root, width=700)
- Content.pack(side=TOP)
- #========================================LABEL WIDGET===========================
- lbl_title = Label(Top, text="Simple PDF Generator", width=700, font=('arial', 18))
- lbl_title.pack(fill=X)
- lbl_text = Label(Mid, text="Title", font=('arial', 18))
- lbl_text.grid(row=0, column=0)
- lbl_content = Label(Content, text="Content", font=('arial', 18))
- lbl_content.grid(row=0, column=0)
- #========================================Text WIDGET===========================
- title = Entry(Mid, textvariable=TITLE, width=10, font=('arial', 18))
- title.grid(row=0, column=1)
- content = Text(Content, height=10, width=22)
- content.grid(row=1, columnspan=5, padx=10, column=0)
- #========================================BUTTON WIDGET==========================
- btn1 = Button(Content, text="Save", width=20, command=Save)
- btn1.grid(row=3, columnspan=5, column=0, pady=10)
Creating the Main Function
This is the main function where the PDF generator is being processed. To do that just simply copy the code below then paste it inside the IDLE text editor.- #========================================VARIABLES==============================
- TITLE = StringVar()
- CONTENT = StringVar()
- #========================================METHODS================================
- def Save():
- CONTENT = content.get("1.0", "end-1c")
- c = canvas.Canvas("%s" % TITLE.get()+".pdf")
- c.drawString(30,800, "%s" % CONTENT)
- c.save()
- TITLE.set("")
- content.delete('1.0', END)
Initializing the Application
After finishing the function save the application as index.py. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.- #========================================INITIALIZATION===================================
- if __name__ == '__main__':
- root.mainloop()
Add new comment
- 256 views