How to remove the first or last page of a .pdf using a Mac

I needed to remove the first and last pages of a hold lot of .pdf files on a Mac. It wasn’t that easy to find solutions and so I thought I would document the process:

1 – Install pdftk server which rather helpfully has a Mac installer:

https://www.pdflabs.com/tools/pdftk-server/

Run the commands
To remove the first page of all the .pdf files in a directory use this command:

for i in *pdf ; do pdftk $i cat 2-end output output/$i ; done

To remove the last page of all the .pdf files in a folder use this command:

for i in *pdf ; do pdftk $i cat 1-r2 output trimmed/$i ; done

Hope this helps someone.

How to split a .pdf every 2 pages using Python

I’ve just scanned in some old copies of Trail walking magazine. I had around 150 .pdf files in a directors and needed to break the split them every two pages. I thought my python script might be useful to someone. It used pypdf which you easierly install using:

pip install pypdf

The script is as follows:


#!/usr/bin/env python

from pyPdf import PdfFileWriter, PdfFileReader
import glob
import sys

pdfs = glob.glob("*.pdf")

for pdf in pdfs:

    inputpdf = PdfFileReader(file(pdf, "rb"))

    for i in range(inputpdf.numPages // 2):
        
        output = PdfFileWriter()
        output.addPage(inputpdf.getPage(i * 2))

        if i * 2 + 1 <  inputpdf.numPages:
            output.addPage(inputpdf.getPage(i * 2 + 1))

        newname = pdf[:7] + "-" + str(i) + ".pdf"

        outputStream = file(newname, "wb")
        output.write(outputStream)
        outputStream.close()

I hope this helps someone.

paramiko.SSHException: Incompatible ssh peer (no acceptable kex algorithm) ubuntu 14.04

I was trying to sftp some files using Python Paramiko on Ubuntu 14.04 and got the following error: “paramiko.SSHException: Incompatible ssh peer (no acceptable kex algorithm)”. It turns out that there is an incompatibility issue with OpenSSH 6.7 and Paramiko with a version less than 1.15.1. At the time of writing (November 2014) Ubuntu 14.04 came with 1.10.1.

To fix the issue, you need to upgrade Paramiko to at least 1.15.1. You can do this by using PIP which is a Python package management system. Here is how to you do this:

apt-get install python-pip

Do a quick version check of the Python libs like so:

pip freeze

Which gives the following output:

landscape-Client==14.01
PAM==0.4.2
Twisted-Core==13.2.0
Twisted-Names==13.2.0
Twisted-Web==13.2.0
apt-xapian-index==0.45
argparse==1.2.1
chardet==2.0.1
colorama==0.2.5
configobj==4.7.2
html5lib==0.999
mercurial==2.8.2
paramiko==1.10.1
pyOpenSSL==0.13
pycrypto==2.6.1
pycurl==7.19.3
pyinotify==0.9.4
pyserial==2.6
python-apt==0.9.3.5
python-debian==0.1.21-nmu2ubuntu2
requests==2.2.1
six==1.5.2
ssh-import-id==3.21
urllib3==1.7.1
wsgiref==0.1.2
zope.interface==4.0.5

Then upgrade Paramiko using PIP.

pip install paramiko --upgrade

Voila. Paramiko sftp should now work.

In my case, I actually use Ansible to deploy changes across multiple servers. Ansible comes with a PIP module. You can automatically upgrade Paramiko across all servers like so:

## Python Packages Using PIP 
## We are doing this because we require the latest version of paramiko
- pip: name=paramiko state=latest

How to setup Python Twitter Tools

I’m just playing around with Python Twitter Tools but given that I am new to Python, I can’t work out to set it up.

Step 1: Download and unzip the files from: https://github.com/sixohsix/twitter

Step 3: Install python-pip on your computer which is a tool for installiing Python software:

apt-get install python-pip

Step 4: Browse the folder and run:

apt-get install twitter

Step 5: Then you are can just run

twitter

The software will then authenticate with Twitter. You just have to login and get the pin number for the application.

Enjoy.

How to pipe Apache logs to a python script

I wanted to pipe the output of my Apache’s error logs to a Python script. To do this you need to set the a CustomLog directive in your Apache Config.

Edit /etc/apache2/sites-available/default and add the following line:

CustomLog "| /usr/bin/yourscript" combined

To read the input into Python you need to setup a non-blocking read from stdin.

#! /usr/bin/python
import sys
import select

while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
    line = sys.stdin.readline()

    if line:
        # Read a line
        exit()

    else:
        # End of file
        exit()
else:
    # Nothing to read
   exit()

Obviously, this script just exit’s at end point but you could put logic at each point to process the text as it is read, not read or eof by python.

Let me know if you have any improvements to this script.