pdftag.py :  » Media-Sound-Audio » Python-Audio-Tools » audiotools-2.14 » docs » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Media Sound Audio » Python Audio Tools 
Python Audio Tools » audiotools 2.14 » docs » pdftag.py
#!/usr/bin/python

#this program uses pdftk to add Title and Author tags to PDF files

import sys
import subprocess
import tempfile
import optparse

if (__name__ == '__main__'):
    parser = optparse.OptionParser()
    parser.add_option('-t','--title',dest='title',
                      help='the PDF\'s Title field')
    parser.add_option('-a','--author',dest='author',
                      help='the PDF\'s Author field')

    (options,args) = parser.parse_args()
    if (len(args) < 1):
        print >>sys.stderr,"*** at least 1 PDF is required"
        sys.exit(1)

    fields = {}
    if (options.title is not None):
        fields["Title"] = options.title
    if (options.author is not None):
        fields["Author"] = options.author

    for pdf in args:
        #grab the PDF's current Info data
        dump = subprocess.Popen(["pdftk",pdf,"dump_data"],
                                stdout=subprocess.PIPE)
        info_data = []

        #filter out any duplicate keys already in "fields"
        line = dump.stdout.readline()
        while (len(line) > 0):
            if (line.startswith("InfoKey:") and
                (line[len("InfoKey:"):].strip() in fields.keys())):
                dump.stdout.readline() #skip InfoValue also
            else:
                info_data.append(line)
            line = dump.stdout.readline()
        if (dump.wait() != 0):
            print >>sys.stderr,"*** Error dumping data with pdftk"
            sys.exit(1)

        temp_info = tempfile.NamedTemporaryFile(suffix=".info")
        temp_pdf = tempfile.NamedTemporaryFile(suffix=".pdf")
        try:
            #dump our new fields and existing data to a temporary info file
            for field in ["Title","Author"]:
                if (field in fields.keys()):
                    temp_info.write("InfoKey: %s\n" % (field))
                    temp_info.write("InfoValue: %s\n" % (fields[field]))

            for line in info_data:
                temp_info.write(line)
            temp_info.flush()

            #use pdftk to add the info file's contents to a temporary PDF
            if (subprocess.call(["pdftk",pdf,"update_info",
                                 temp_info.name,
                                 "output",temp_pdf.name]) != 0):
                print >>sys.stderr,"*** Error transferring data with pdftk"
                sys.exit(1)

            #copy the temporary PDF over the original PDF
            if (subprocess.call(["cp","-f",temp_pdf.name,pdf]) != 0):
                print >>sys.stderr,"*** Error copying updated PDF over original"
                sys.exit(1)
        finally:
            #close temporary files
            temp_info.close()
            temp_pdf.close()

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.