Python PDF Generator

This application was initially meant to be written over the period of a few weeks, but took only a few hours. The original goal was just to make sure I'm hitting the word limit the professor wanted on discussion posts. I hit that goal pretty quickly, and this project morphed into something much larger. Nowadays it can generate PDFs on top of typical functions, and the code is a little prettier. Let's take a look at the code.

Import statements for the edior

Everything else here on out is related to the graphical user interface or GUI (pronounced: Gooey). The first step to creating a graphical user interface is to define the main window. Then each individual item that is going to be added to the main window has to be "packed" to that window. Lastly, I created an array to store all of the text from the textbox. This step is probably not entirely necessary, but it makes the code easier to read and a little more straightforward.

defines the main window, initializes elements of the window to be added onto the window, and makes the text in the textbox created in the window programmatically accessible

The next function just lets me call a file dialog box to open files on demand later on when I implement the menu.

Function to call a file explorer to open files on demand

This function is basically more of the same, except it will let me save files on demand later on when I implement the menu.

Function to call a file explorer to save files on demand

The following function is the one that enables exporting to PDF. Some of the values that are hardcoded could be made changeable with variables. I just did not feel like doing that kind of work. This app is not really meant to compete with Microsoft Word.

Function to enable exporting the text in the text box to PDF

The next series of statements simply let me create a menu for the window. The "filemenu" items are added to the menubar at the end, and with a cascading effect so that the items are listed vertically.

Statements to attach functions performing actions to menu buttons

Now we are getting to the fun parts of the application. This next function pulls out all of the words from the textbox using a regular expression (regex) pattern that will tell it to get only letters or letters separated by hyphens. Letters separated by hyphens or that include apostrophes are counted as a single word because they are included in the pattern, and the addition sign at the end instructs the computer to keep going until it hits punctuation or spaces.

A function that uses a regular expression pattern and array to pull words out of the text and store it for access programmatically.

This next snippet of code merely creates a popup later on that will display the analysis performed on the text.

A function to call a popup on demand

Between the merger of this application this function easily underwent the largest change. The first thing I do is set up separate arrays to keep track of each unique word and how many times that word appears (count array). Then I keep track of the index of each unique word and associate it with an index in the count array, which is incremented each time that word is seen. Later on I go through both of these arrays to create objects of the 'Word' class so that both the amount of times the word appears in the text, and the word itself are closely associated. By doing this I'm able to reduce the arrays down to a single array, which can be sorted using Python's built-in function. However, it is important that these values are sorted by the amount of times they appear in the text so I give the sorted function an unnamed function call telling it to sort in reverse (decreasing) order based on the count attribute.

A function to determine how often words uniquely appear in a text and sort them in descending order

The next three functions were covered above. They just provide popups on demand and add more stuff to the menu.

creates a menubar for the analytical options and puts them in vertical order Two popups that help break down the options and give information about the author Another series of commands to add stuff to the menu and a loop so that the window will stay open as long as the application is in use.

This final bit of code is the class objection definition that makes sorting the array possible. All I do is define two variables to keep track of the word and how many times it appears. Then I define a constructor that lets me assign an initial value to both of those variables. Usually classes have methods to alter the class members instead of allowing direct access, but Python doesn't really have a private scope like C++ does. So I left them out because I did not think they would add much at all.

A simple class definition that associates both the amount of times a word appears in a text, and the word itself together as a single object.

Lastly, the script is available here, and the demo video is available below. The text I chose analyze was The Fog by William Dudley Pelley because it looked massive.