Sunday, March 17, 2019

Python Code Snippets #13

Python Code Snippets #13

python-code-snippets-13-logo

61-Extract images from PDF

How cool is this little snippet? It rips all the JPEG’s from a PDF file. I tested this on several PDF’s, and they all worked great.

I lifted the code from a very good Python blog by Ned Batchelder, so all credit goes to him. All I did was make it Python 3.

Don’t forget to put the PDF to rip in the current directory, or give the file location in the source code.

It would be very easy to make a simple GUI for this, it could have a file selector to load the PDF, and save location for the images.

python-code-snippets-13-extract-images-from-pdf

Double-click inside source code box to select all.

'''
Extract JPEG's from PDF's. Quick and dirty.

Edited by Shambles.
From a 2007 article by Ned Batchelder.
https://nedbatchelder.com/blog/200712/extracting_jpgs_from_pdfs.html

More Python atrocities at:
https://stevepython.wordpress.com/
'''

# Requires a PDF file in current dir.
with open("test.pdf", "rb") as file:
    pdf = file.read()

startmark = b"\xff\xd8"
startfix = 0
endmark = b"\xff\xd9"
endfix = 2
i = 0

njpg = 0
while True:
    istream = pdf.find(b"stream", i)
    if istream < 0:
        break
    istart = pdf.find(startmark, istream, istream + 20)
    if istart < 0:
        i = istream + 20
        continue
    iend = pdf.find(b"endstream", istart)
    if iend < 0:
        raise Exception("Didn't find end of stream!")
    iend = pdf.find(endmark, iend - 20)
    if iend < 0:
        raise Exception("Didn't find end of JPG!")

    istart += startfix
    iend += endfix
    print("JPG %d from %d to %d" % (njpg, istart, iend))
    jpg = pdf[istart:iend]
    with open("jpg%d.jpg" % njpg, "wb") as jpgfile:
        jpgfile.write(jpg)

    njpg += 1
    i = iend

62-Sort a list of names by surname, in one line of code

Credit for this cool little lambda snippet goes to Chris Albon, in this article. As I say in the source, I meddled with it to try and explain how it works, and changed the list of names to some characters from a favourite comedy show of mine, Toast Of London. See video at end of this post.

python-code-snippets-13-sort-by-surname

'''
    Sort a list of names by surname in one line, using lambda.

    Borrowed from this blog:
    https://chrisalbon.com/python/basics/

    Made slightly amusing, and palatable for beginners by:
    Steve Shambles March 2019

    More Python atrocities at:
    https://stevepython.wordpress.com/
'''

# A list of names, could be loaded from a file,
# or inserted like this:
some_names = ["Steven Toast", "Ray Purchase", "Jane Plough",  \
"Susan Random", "Clem Fandango", "Clancy Moped"]

# Sort names by surname.
# This works by splitting the string at the space
# and taking the last element [-1]
# which the sort routines works on.
print(sorted(some_names, key=lambda x: x.split(" ")[-1]))

63-Replace defined chars in a string

I learnt how to do this somewhere, ages ago. I have no idea where. It could be useful one day I suppose?

python-code-snippets-13-replace-defined-chars-in-string

'''
   Using Maketrans and translate to replace parts of strings.

   By Steve Shambles March 2019.
   For more incompetent code visit:
   https://stevepython.wordpress.com
'''

# Example, make all vowels uppercase.
# Change these to anything you want,
# but must be same length.
replace_these = "aeiou"
with_these = "AEIOU"

tranny = str.maketrans(replace_these, with_these)

text_string = "You can replace any character with any other using maketrans."
print(text_string.translate(tranny))

64-Retrive last modified date of a file

This is just something I saw in some source code, somewhere. Again, whether it will be useful in one of my, (or your), programs remains to be seen.

python-code-snippets-13-last-modified

'''
    Retrieve last modified date stamp from a file

    Code borrowed with thanks from a great Python
    site: https://thispointer.com
    I Hope they don't mind!

'''

import os
import time
import stat

# file path and name to inspect
fileStatsObj = os.stat ("test.pdf")

modificationTime = time.ctime ( fileStatsObj [ stat.ST_MTIME ] )
print("Last Modified Time : ", modificationTime )

65-Unzip a zip archive

This has a few potential uses, in an installer for example. It’s got o be useful to have this in any code collection.

'''Extract all files from a zip archive'''

import zipfile

# Requires a zip archive called 'test.zip'
# in current directory.

un_zip = zipfile.ZipFile('test.zip')
un_zip.extractall()

# Will unzip contents of test.zip
# in same directory as .zip file.

That’s all for now. I will be back very soon with Snippets #14.

Using Python V3.6.5 32bit, on Windows 7 64bit

Don’t forget to check out my other snippets:

Snippets #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11 and #12

You can get all my source code snippets and projects from my Dropbox folder.


I have also started another free blog of my personal Memoirs, that nobody will be interested in, except maybe my son and my mum 🙂

“If one does not leave a record of ones doings, then what is the point of it all?”

Steve Shambles.

Advertisements


from Python Coder
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...