Merging PDF Files In Linux Using PyPDF
PyPDF is a handy and valuable Python library for merging and splitting PDF files in Linux. It’s pure Python library built as a PDF toolkit. It is capable of:
- extracting document information (title, author, …),
- splitting documents page by page,
- merging documents page by page,
- cropping pages,
- merging multiple pages into a single page,
- encrypting and decrypting PDF files.
PyPDF is a great Python library use by many Python applications which handles PDF files directly. PDF-Shuffler is a one of the tools written based on PyPDF which you can use to merge PDF files easily in Linux. In Ubuntu you can install it using following command.
Here is a sample code that merge PDF files together using PyPDF library. In this code I have used PdfFileWriter and PdfFileReader classes from PyPDF module to read and append PDF files together. This sample doesn’t contain completed error handling logic for file handling.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from pyPdf import PdfFileWriter, PdfFileReader
def mergePDFFiles(outputFile, filesToBeMerged):
output = PdfFileWriter()
if(len(filesToBeMerged) == 0):
print 'Empty Input File List'
return;
for inFile in filesToBeMerged:
print 'Adding file' + inFile + ' to the out put'
# Read the input PDF file
input = PdfFileReader(file(inFile, "rb"))
# Add every page in input PDF file to output
for page in input.pages:
output.addPage(page)
print 'Writing the final out put to file system'
# Out put stream for output file
outputStream = file(outputFile, "wb")
output.write(outputStream)
outputStream.close()
if __name__ == '__main__':
i = 0
outputFile = ''
inputFiles = []
for arg in sys.argv:
i = i + 1
# Getting out file
if arg == '-o':
outputFile = sys.argv[i]
print 'Output File: ' + outputFile
# Extracting Input files
if arg == '-i':
outfileOptionPos = sys.argv.index('-o')
if i < outfileOptionPos:
inputFiles = sys.argv[i: outfileOptionPos]
filesStr = ",".join(inputFiles).replace(",", " ")
print 'Input Files: ' + filesStr
else:
inputFiles = sys.argv[i:]
filesStr = ",".join(inputFiles).replace(",", " ")
print 'Input Files: ' + filesStr
# Merging PDF files
print 'Merging PDF Files......'
mergePDFFiles(outputFile, inputFiles)
You can get more understanding about usages of PyPDF if you explore more about open source projects which uses PyPDF. Here are some of the projects which use PyPDF.
Related Resources:
January 16, 2010 1 Comment
Using libnotify in Ubuntu 9.04
There are some applications which need to send notifications to users. Ubuntu 9.04 has new on-screen-display notification agent called Notify OSD which implmeents freedesktop.org Desktop Notifications Specification with semi-transparent click-through bubbles. This post is not about Notify OSD, but about how you can use libnotify to send notifications in you shell scripts, C programs and Python programs.
Send notifications from your shell scripts
You have to install libnotify-bin package in Ubuntu and you can use notify-send command to send notifications from your shell script. After installing libnotify-bin you can try notify-send command just like following.
By default the message will be displayed for 5 seconds. To change how long a message stays displayed use the “-t” switch. You can use “-t 0″ leave the message up until the user closes it. But in new Ubuntu notification agent will display these notifications as alert box. You can find great explanations about Notify OSD here.
notify-send "Click me to close me." -t 0
Add title to your notification like following:
Add icon with title and message body(You can find icon codes here):
Send notifications from your Python application
You need to install python-notify package first and then you can use pynotify to send notification from your Python script. There are different capabilities in different notification agents. I am ignoring getting those information in this example.
import sys
import pynotify
if __name__ == "__main__":
# Check whether your notification agent support
# incon-summary-body layout.
if not pynotify.init("icon-summary-body"):
sys.exit(1)
n = pynotify.Notification(
"You have a mail",
"You have new e-mail from Milinda",
"notification-message-email")
n.show()
Sample code in C
You need to install libnotify-dev, libglib2.0-dev and libgtk2.0-dev packages first. You can compile this sample using following gcc command.
#include
#include
void closed_handler (NotifyNotification* notification, gpointer data){
g_print ("closed_handler() called");
return;
}
int main(int argc, char** argv){
NotifyNotification* notification;
gboolean success;
GError* error = NULL;
if (!notify_init ("icon-summary-body")){
return 1;
}
notification = notify_notification_new (
"You have a mail",
"You have mail from Milinda",
"notification-message-email",
NULL);
error = NULL;
success = notify_notification_show (notification, &error);
if (!success)
g_print ("That did not work ... \"%s\".\n", error->message);
g_signal_connect (G_OBJECT (notification),
"closed",
G_CALLBACK (closed_handler),
NULL);
notify_uninit ();
return 0;
}
For more information please refer Ubuntu Notification Development Guidelines document.
September 12, 2009 4 Comments