Python was one of the first languages I learned, but I disregarded it after I realized the syntax was a bit weird for me compared to C-based languages which I am more comfortable with. But Python is a popular language, one that is not close to declining. So whether I like it or not I chose to return to it.
That led me to this, a challenge to me at the time, I had to use Python with the React JS application I was building, of course, the only reason I was limiting myself to using Python was to push myself. Many languages had richer libraries I could use to solve my problem like dotNET.
The application I was trying to build was a file converter, and the first feature I chose to implement was a pdf to word converter. Building my converter would require good knowledge of binaries, parsing, etc. I chose to use a Python module called pdf2docx.
from pdf2docx import Converter
import os
import sys
pdfFilePath = sys.argv[1]
if os.path.exists(pdfFilePath):
pathLength = len(pdfFilePath)
newPath = './converted/new.docx'
cv = Converter(pdfFilePath)
cv.convert(newPath)
cv.close()
else:
print("\n----NO SUCH DIRECTORY!----")
Difficulties
Here are some of the issues I faced down the line:
- How do I use Python with the React JS app?
- What’s the best way to send the file from the front to the back end?
Solving item 1
I explored various solutions like using Python on the back-end with frameworks like Flask or Django. But that would be overkill for one simple functionality.
Although not a novel solution and for many of you reading this you would have never considered this a problem, but for me a baby programmer it was a bit different. The first thing to consider was a server. I am not used to back-end programming with Python nor see any reason to use Python's HTTP server in this project. I decided to use node with express as it was a straighter forward implementation for me. So now the project had two servers, the Vite server for front-end rendering and Express for working at the back. I decided to use “child_process”;
const {spawn} = require("child_process");
a node module for creating sub-processes. In child_processes there is a function spawn, which is used to run scripts like how you would in a terminal.
const pythonProcess = spawn("py", ["./c.py", `${filePath}`]);
Solving item 2
The second issue was accessing the file on the backend. The solution was quite simple but I only embarrassingly figured it out after crying and threatening my computer. Originally, I had used the regular file picker input in HTML to allow the user to select a PDF file to convert, but I changed it to a drag-and-drop system. When the user drops their file in the “drop region” it needs to be passed as an argument to the python script which does the conversion. A fetch method is called to send the file path to the node server. Using this file path and the “child_process” a Python script is executed and the pdf file is converted to a docx file. The new file is automatically saved in the root folder called “converted”.
Finally
I am taking on more small projects like this to build my familiarity with various programming tools. The goal is to be able to confidently and efficiently solve simple to moderately difficult programming tasks. To be able to hop on the computer and automate renaming my files, or creating an app to monitor my sitting posture through my camera and to do it on a whim. That is why I decided to take on an internship program called the HNG internship (https://hng.tech/internship) which encourages language fluency and familiarity with various organizational skills and methods through individual projects and group projects. Although we are just at the first stage, stage 0, I can already see the detail and deliberate decisions in the tasks set. If you would like to Join I advise you to go with the paid option at https://hng.tech/premium to get a certificate upon completing the program and access the community for up to one year after the program ends.
Cover photo by mark glancy: https://www.pexels.com/photo/boston-terrier-wearing-unicorn-pet-costume-1564506/
Top comments (0)