couleur de fond d'evince

Avell Diroll avelldiroll at gmail.com
Sam 30 Jan 09:25:38 GMT 2010


Avell Diroll wrote:
(...)
> Pour ceux qui ne peuvent pas faire tourner compiz ou qui le trouve trop 
> lourd, voici un petit script qui permet de réaliser une copie d'un 
> document pdf en changeant la couleur du fond (par défaut en gris).
(...)

Quelques corrections après une seconde lecture ...

##############pdf_background.py############################
#!/usr/bin/env python

# This script require:
# *python: http://python.org/
# *PIL: http://www.pythonware.com/products/pil/
# *pyPDF: http://pybrary.net/pyPdf/

# pdf_background.py
# Copyright (c) 2010 Avell Diroll <avelldirolll at gmail.com>

"""pdf_background.py
This is a quick and dirty script to change the background colour of pdf 
files"""

__author__ =    "Avell Diroll <avelldirolll at gmail.com>"
__version__ =   "0.2"
__date__ =      "2010-01-29"
__copyright__ = "Copyright (c) 2010 %s. All rights reserved." % __author__
__licence__ =   "BSD"


from PIL import Image
from pyPdf import PdfFileWriter, PdfFileReader
from optparse import OptionParser
import sys
import tempfile


def change_background(input_file, output_file, background_colour):
     """add a coloured background to input_file and save it to 
output_file"""

     original = PdfFileReader(open(input_file))
     output = PdfFileWriter()

     for page_number in range(original.getNumPages()):
         page_width, page_height = 
original.getPage(page_number)['/MediaBox'][2:4]
         page_width = int(round(page_width))
         page_height = int(round(page_height))

         background_file = tempfile.NamedTemporaryFile()
         background = Image.new("RGB", (page_width, page_height), 
background_colour)
         background.save(background_file, "PDF")

         current_page = original.getPage(page_number)
         background_page = PdfFileReader(background_file)
         page = background_page.getPage(0)
         page.mergePage(current_page)
         output.addPage(page)

     output_stream = open(output_file, "wb")
     output.write(output_stream)
     output_stream.close()


def main():
     """Parse command line and launch process"""

     # create the options we want to parse
     usage = "\n%%prog [options] pdf_file \n\n%s" %__doc__
     version="%%prog %s %s" %(__version__, __date__)
     optParser = OptionParser(usage=usage, version=version)
     optParser.add_option("-c", "--colour", type="string",
           dest="background_colour",
           default="#aaaaaa", metavar="#aaaaaa",
           help="background colour - hexadecimal RGB value")
     optParser.add_option("-s", "--suffix", type="string", dest="suffix",
           default="-with-bg",
           help="Suffix to append at the end of the output filename, "
           "default: -with-bg")
     (options, args) = optParser.parse_args()

     # check that they passed in atleast one file to process
     if len(args) != 1:
         optParser.error("Process one file at a time\n")

     input_file = args[0]
     output_file = args[0][:-4] + options.suffix + ".pdf"
     background_colour = options.background_colour

     change_background(input_file, output_file, background_colour)
     return 0


if __name__ == '__main__':
     sys.exit(main())





Plus d'informations sur la liste de diffusion ubuntu-fr