Tag: python

Python Program to Pip Module. Packaging a python script/s as installable module.


Python is a widely used programming language that provides numerous libraries and modules for various purposes. In this article, we will explore the process of converting a Python script into a pip installable module. This allows users to easily install and utilize the functionality provided by your script, enhancing code reusability and simplifying integration into other projects.

What is pip?

Before diving into the details of converting Python scripts into pip installable modules, let’s briefly discuss what pip is. Pip is a package management system used to install and manage software packages written in Python. It automates the process of downloading, installing, upgrading, and managing dependencies of Python packages, making it a valuable tool for developers.

Creating a Python Package

To convert a Python script into a pip installable module, the first step is to transform it into a Python package. A package is a collection of modules and sub-packages that provide a specific functionality. To create a package, follow these steps:

  1. Create a directory with a name that represents your package. This name should be unique and descriptive.
  2. Within the package directory, create a file named __init__.py. This file serves as an indicator that the directory should be treated as a package.
  3. Place your Python script(s) inside the package directory. These scripts will form the core functionality of your module.

Writing a setup.py File

To make your package installable via pip, you need to provide a setup.py file. This file contains metadata about your package, such as the package name, version, author, and dependencies. Here’s an example setup.py file:

from setuptools import setup

    author="Your Name",
    description="A brief description of your package",

Make sure to replace "your-package-name" with the actual name of your package and adjust the other fields accordingly.

Installing and Distributing the Package

With the setup.py file in place, you can now install your package using pip. Open your terminal or command prompt and navigate to the directory where your setup.py file is located. Run the following command to install your package:

pip install .

By including the dot (.) at the end of the command, pip will install the package from the current directory.

To distribute your package, you can upload it to the Python Package Index (PyPI) or any other package repository of your choice. This allows users to easily install your package using pip from any location.


Converting your Python script into a pip installable module is a valuable process that enhances code reusability and simplifies integration into other projects. By following the steps outlined in this article, you can create a Python package, provide the necessary metadata in the setup.py file, and install and distribute your package using pip. This empowers users to leverage your module’s functionality with ease and contributes to a more efficient and collaborative programming ecosystem.

Remember, packaging your script as a module adds value to the Python community by enabling others to utilize and build upon your code. So, start converting your Python scripts into pip installable modules and share your contributions with the world!

Generate PDF using python and HTML


sudo pip3 install pdfkit
sudo apt-get install wkhtmltopdf

Pdfkit and wkhtmltopdf are the packages that you will be required before getting started.

PDF can be generated in 3 ways. One is using a website url, second one is using a html string and the last one is using a html file(optional css file can also be specified in all 3 ways).

In this article we will discuss the third way which is more advanced one and will be most useful for most of our requirements. Below is the program I am going to use to generate the PDF.

import pdfkit
import sys

inputfile = sys.argv[1]
outfile = args.argv[2]

options = {
      "enable-local-file-access": True,
      "enable /var/www/html/": True,
    'page-size': 'Letter',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'custom-header': [
        ('Accept-Encoding', 'gzip')
    'cookie': [
        ('cookie-empty-value', '""'),
        ('cookie-name1', 'cookie-value1'),
        ('cookie-name2', 'cookie-value2'),
    'no-outline': None
# Single CSS file
css = 'bootstrap.css'

#pdfkit.from_url('https://google.com', 'out1.pdf')
pdfkit.from_file(inputfile, outfile, options=options, css=css)
#pdfkit.from_string('Hello!', 'out3.pdf')

In the code we can notice at the bottom that we can use a URL, local html file or a plain html string. Also a bootstrap.css file is being used for our styles. Common options that can be used to configure the PDF can also be seen in the above code. For full list of options that can be used read the documentaion here.

Now this code takes two arguments first one is input html file and the other being name of the output pdf that you want to generate into.

More documentaion regarding WKHTMLTOPDF can be read here.

Python program for Generating pdf using html content

Python replace a string from Dictionary/Hash mapping

In a situation where we have a dictionary mapping and want to use the mapping to replace a particular string we can use the below code to do the same.

Have a look at below code:

import re

my_dict = {
    "\u0c1C" : "ja",
    "\u0c15" : "ka"

string = "కరజ"

for unicode, roman in my_dict.items():
    string = string.replace(unicode, roman)


The above code will replace the string from the dictionary. Note that the dictionary has unicode code points that are mapped to specific values. This code will be useful for natural language processing when we deal with unicode range points.

Perl and Python program syntactic differences

If you know Perl Programming then learning Python is not that hard. But many times we would be confusing Perl syntax for Python and might get into trouble while writing Python code. So here I am listing some differences that I know and will keep adding as I knew it.

Variable names

In Perl we would save a variable like $my_variable/@my_varibale etc, but in Python we just can write the names nothing else needed.

Regex Substituion

In Perl we write $in=~s/to/too/g; In Python it is in = re.sub(r'to', 'too', in). Note the difference? In Perl substituion happens in same variable in Python we explicitly need to specify in which variable we are saving the substitution. To replicate the Perl code we have saved in same variable.


In Perl to import modules we write as use 'modulename' in Python we write as import re. To install module in Perl we have cpan and in Python we use Pip to install modules.

Hash/Dictionary usage

In Perl to assign value to a key we write as $hash{$key} = $value in Python we write as hash[key] = value

Code Indendataion

Perhaps this is the major difference between Python and many other major programming languages. In Perl we have to use curly braces to enclose a block of code into a function/loop/if-else-block in Python we need to indent properly. Also no semicolons are needed to tell the compiler that it is the end of the line we need to just press enter after a line of code. This allows code to look very readable.

Linking a Python script to a web interface with file upload.

In this article I am going to explain how can we link a python script with a web interface. In case you need the GIT hub link for the entire project you can find it here.


  • PHP
  • Web Server(apache)
  • Javascript
  • A Python script that you want to link with the web interface.

The Python Script

#convert text to lowercase 

import sys
#open file using open file mode
fp1 = open(sys.argv[1]) # Open file on read mode -- input file
lines = fp1.read().split("\n") # Create a list containing all lines
fp1.close() # Close file

#write output to a file name it out.txt
fp2 = open("out.txt", "w")

#do something with the text and write it to the file
for line in lines:
	out_line = line.lower()
	fp2.write(out_line + "\n")


This is our Python script that we want to use it from a Wen Interface where we give input text and the lower case text is shown in the web page itself. Remember I am here just taking a very small example this python script could be doing anything but the basic point is that it takes an input file with some text and writes the output to another file.

To make sure this works, run this python script independently and see if it works as expected.

python3 case_convert.py in.txt
  • Replace your python script with your code.
  • If you have any other script like in PERL, Shell script even then it works fine. You just need to change it in PHP script where it calls the system command.

This script takes a input file in.txt that contains some text and it will be converted to lower case when we execute the script and save it to a output file out.txt

Now lets build our Web page we will call it as index.html It will look like below.

			A Web Interface

		<script src="js/jquery.js"></script>
		<script src="js/jquery-ui.js"></script>
		<script type="text/javascript" src="config.js"></script>
		<script type="text/javascript" src="js/FileSaver.js"></script>
		<script type="text/javascript" src="js/validation.js"></script>

		<link rel="stylesheet" type="text/css" href="bootstrap-3.3.5-dist/css/bootstrap.css" />
		<link rel="stylesheet" type="text/css" href="css/styles.css" />
		<link rel="stylesheet" type="text/css" href="css/responsive.css" />

		<script type="text/javascript" src="bootstrap-3.3.5-dist/js/bootstrap.js" ></script>
		<script type="text/javascript" src="js/convert.js" ></script>

		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
		 <meta charset='utf-8'>

				<h4 align="center">Convert Upper case to LowerCase </h4>
			<!--common for file and text output-->
			<!--upload form-->
			<form class="form-inline" id="upload" method="post" enctype="multipart/form-data">

				<div class="form-group col-12 col-m-12">
					<label class="btn btn-primary" for="my-file-selector">

						<!--<input accept=".doc,.rtf,.docx,.pdf,text/plain" type="file" id="file" name="inputfile" multiple/>-->
						<input accept=".pdf,text/plain,.doc" type="file" id="file" name="inputfile" onchange="startRead(this)" multiple  multiple/>

					<!--<button type="button" class="form-control btn btn-default dropdown-toggle" data-toggle="dropdown">
						Select Business type <span class="caret"></span>
				<!--	<span id="mandatory">*</span> <select name="srclanguage"  id="srclanguage" class="form-control"><option value="" >Choose Type</option>
						<option disabled="true"></option>

					<button id="submit" type="button" class="btn btn-default navbar-btn" onclick="submittext();">
					<img style="margin-left:2%;height:4%;position:relative;" id="loading" src="images/processing.gif"></img>
			<textarea id="input"></textarea> 
			<textarea id="result"></textarea> 
					<button id="edit" type="button" class="btn btn-default navbar-btn" onclick="edittext();">
					<button id="sample1" type="button" class="btn btn-default navbar-btn sample" onclick="sample(this.id);">
					<button id="sample2" type="button" class="btn btn-default navbar-btn sample" onclick="sample(this.id);">
					<button id="sample3" type="button" class="btn btn-default navbar-btn sample" onclick="sample(this.id)">
					<button id="clear" type="button" class="btn btn-default navbar-btn" onclick="empty();">
					<select style="background-color:#7ba733;color:#fff;" name="savetype"  id="savetype" class="form-control"><option value="" >Export to</option>
						<option disabled="true"></option>
						<option value="txt">.txt </option>
						<option value="csv">.csv </option>
						<option value="doc">.doc</option>
						<option value="rtf">.rtf</option>
						<option value="pdf">.pdf</option>

					<button id="download" type="button" disabled class="btn btn-default navbar-btn" onclick="saveasfile();">

				</div> <!-- end of form group div -->


			<div class="progressbar">
				<div class="bar">

			</div> <!-- end of progressbar div-->

			<!--<div contenteditable="true" id="result"></div>--> <!-- end of result div-->
			<!--			<textarea onkeypress="txtAreaId(this.id);" id="result-text"></textarea> -->

		</div>	<!-- end of container div -->

Looks like a lot but it simply does hold two textareas, button for submitting input text, downloading the output text and linked Javascript and CSS files.

The main Javascript file convert.js which is in js folder will call a PHP script that will execute our python program. I will just list the main function here that is activated when we click submit after inputting some text.

//called when submit button button is clicked
function submittext(){

	//retrieve values from fields
	var srctext = $("#input").val();
	//alert(fromto+" "+srctext);
	if(typeof srctext =="undefined" || srctext =="") {
		alert("Provide some text...");
		return false;

	//Ajax call to upload and submit for conversion
		type: 'POST',
		url: "scripts/convert.php",
		//data: "&from=" + from + "&to=" + to + "&text=" + srctext,
		data: "&text=" + srctext,
		success: function (data) {
			var tgttext = data;
			$('#download').prop('disabled', false);
			$('#language').prop('disabled', false);
		error:function  (jqXHR, exception) {
			var msg = '';
			if (jqXHR.status === 0) {
				msg = 'Not connect.\n Verify Network.';
			} else if (jqXHR.status == 404) {
				msg = 'Requested page not found. [404]';
			} else if (jqXHR.status == 500) {
				msg = 'Internal Server Error [500].';
			} else if (exception === 'parsererror') {
				msg = 'Requested JSON parse failed.';
			} else if (exception === 'timeout') {
				msg = 'Time out error.';
			} else if (exception === 'abort') {
				msg = 'Ajax request aborted.';
			} else {
				msg = 'Uncaught Error.\n' + jqXHR.responseText;
			alert(msg+" Please try afer sometime");
	return false;

So we can see that we are using AJAX which explains why we need jQuery. In the AJax look at the URL it is calling a PHP script convert.php located in scripts directory.

convert.php will look like below. I recommend PHP 7 although it works in lower versions too.

$text = $_POST["text"];

$fp = fopen("in.txt","w");

#call your python script here and make sure your python script is also at same place as this script
$status = system("python3 case_convert.py in.txt");

$fp_out = fopen("out.txt","r");
if ($fp_out) {
    while (($line = fgets($fp_out)) !== false) {
        // process the line read.
        echo "$line";

} else {
    // error opening the file.


After doing all these. You need to give proper permission the scripts directory or just the two files in.txt and out.txt. Below is the command to do the same.

chown www-data:www-data scripts 
chown www-data:www-data in.txt out.txt

After doing this load your web page in the browser. For example I named the directory as basic-web-interface, so in my browser I open it as http://localhost/basic-web-interface/.

If you have PHP/ APACHE and all other javascript and Bootstrap files installed it should load your web page without any hiccups.

Then provide some input text and see that it gets convert to lowercase and the output text is displayed in the other textarea.

  • Check apache error..log file for errors. For ubuntu it is /var/log/apache2/error.log
  • Open Network console(F12) from Browser Developer options and view the API request that is being called. Its request, response, parameters etc.
  • For this Open Developer options, click on network tab and then click submit and view the request/reponse.