nautilus-scripts : compression de PDF
authorProgfou <jean-christophe.andre@auf.org>
Fri, 4 Mar 2016 04:17:48 +0000 (23:17 -0500)
committerProgfou <jean-christophe.andre@auf.org>
Fri, 4 Mar 2016 04:17:48 +0000 (23:17 -0500)
nautilus-scripts/Compression de PDF [new file with mode: 0755]
nautilus-scripts/Conversion PDF vers texte

diff --git a/nautilus-scripts/Compression de PDF b/nautilus-scripts/Compression de PDF
new file mode 100755 (executable)
index 0000000..743478f
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright © 2016 AUF
+# Licence: GPL-2
+# Author: Progfou <jean-christophe.andre@auf.org>
+# Created: 2016-01-21
+# Debian-Depends: python (>= 2.6), ghostscript
+import sys
+import os
+from subprocess import Popen, PIPE
+
+for filename in os.getenv("NAUTILUS_SCRIPT_SELECTED_FILE_PATHS", "").split("\n"):
+    newdir = os.path.dirname(filename)
+    if newdir:
+        olddir = os.getcwd()
+        os.chdir(newdir)
+    filename = os.path.basename(filename)
+    root, ext = os.path.splitext(filename)
+    # FIXME: vérifier plutôt le type MIME
+    if ext.lower() != ".pdf":
+        continue
+    # première version
+    out_filename = root + "-opt1.pdf"
+    p = Popen(["/usr/bin/gs", "-dBATCH", "-dNOPAUSE", "-sDEVICE=pdfwrite",
+               "-q",
+               "-sOutputFile=" + out_filename, filename], stdout=PIPE)
+    # deuxième version
+    out_filename = root + "-opt2.pdf"
+    p = Popen(["/usr/bin/gs", "-dBATCH", "-dNOPAUSE", "-sDEVICE=pdfwrite",
+               "-dQUIET", "-dPDFSETTINGS=/ebook", "-dCompatibilityLevel=1.4",
+               "-sOutputFile=" + out_filename, filename], stdout=PIPE)
+    # retour au contexte initial
+    if newdir:
+        os.chdir(olddir)
+
+sys.exit(0)
index 4a0cbfd..910f22a 100755 (executable)
@@ -6,20 +6,28 @@
 # Created: 2014-01-31
 # Debian-Depends: python (>= 2.6), poppler-utils (pdftotext)
 import sys
 # Created: 2014-01-31
 # Debian-Depends: python (>= 2.6), poppler-utils (pdftotext)
 import sys
+import os
+import os.path
 from subprocess import Popen, PIPE
 
 from subprocess import Popen, PIPE
 
-if len(sys.argv) < 2:
-    sys.exit(0)
-
-for filename in sys.argv[1:]:
-    olddir = os.getcwd()
-    os.chdir(os.path.dirname(filename))
+for filename in os.getenv("NAUTILUS_SCRIPT_SELECTED_FILE_PATHS", "").split("\n"):
+    newdir = os.path.dirname(filename)
+    if newdir:
+        olddir = os.getcwd()
+        os.chdir(newdir)
     filename = os.path.basename(filename)
     filename = os.path.basename(filename)
+    root, ext = os.path.splitext(filename)
+    # FIXME: vérifier plutôt le type MIME
+    if ext.lower() != ".pdf":
+        continue
+    # conversion
+    out_filename = root + ".txt"
     p1 = Popen(["/usr/bin/pdftotext", filename, "-"], stdout=PIPE)
     p2 = Popen(["/bin/sed", "-e", "s|\t\r  | |g;s|-­‐|–|g;s| \+| |g"], stdin=p1.stdout, stdout=PIPE)
     p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
     output = p2.communicate()[0]
     p1 = Popen(["/usr/bin/pdftotext", filename, "-"], stdout=PIPE)
     p2 = Popen(["/bin/sed", "-e", "s|\t\r  | |g;s|-­‐|–|g;s| \+| |g"], stdin=p1.stdout, stdout=PIPE)
     p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
     output = p2.communicate()[0]
-    file(filename + '.txt', 'wt').write(output)
-    os.chdir(olddir)
+    file(out_filename, 'wt').write(output)
+    if newdir:
+        os.chdir(olddir)
 
 sys.exit(0)
 
 sys.exit(0)