nautilus-scripts : compression de PDF
[progfou.git] / nautilus-scripts / Conversion PDF vers texte
CommitLineData
ec45958d
P
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
f7647023
P
3# Copyright © 2014 AUF
4# Licence: GPL-2
5# Author: Progfou <jean-christophe.andre@auf.org>
6# Created: 2014-01-31
7# Debian-Depends: python (>= 2.6), poppler-utils (pdftotext)
ec45958d 8import sys
8b2df3bb
P
9import os
10import os.path
ec45958d
P
11from subprocess import Popen, PIPE
12
8b2df3bb
P
13for filename in os.getenv("NAUTILUS_SCRIPT_SELECTED_FILE_PATHS", "").split("\n"):
14 newdir = os.path.dirname(filename)
15 if newdir:
16 olddir = os.getcwd()
17 os.chdir(newdir)
ec45958d 18 filename = os.path.basename(filename)
8b2df3bb
P
19 root, ext = os.path.splitext(filename)
20 # FIXME: vérifier plutôt le type MIME
21 if ext.lower() != ".pdf":
22 continue
23 # conversion
24 out_filename = root + ".txt"
ec45958d
P
25 p1 = Popen(["/usr/bin/pdftotext", filename, "-"], stdout=PIPE)
26 p2 = Popen(["/bin/sed", "-e", "s|\t\r  | |g;s|-­‐|–|g;s| \+| |g"], stdin=p1.stdout, stdout=PIPE)
27 p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
28 output = p2.communicate()[0]
8b2df3bb
P
29 file(out_filename, 'wt').write(output)
30 if newdir:
31 os.chdir(olddir)
ec45958d
P
32
33sys.exit(0)