ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/PDFExportToDataLoad/Trunk/SingleTiffToLFP.py
Revision: 526
Committed: Fri Feb 21 17:32:01 2014 UTC (12 years, 1 month ago) by nino.borges
Content type: text/x-python
File size: 3350 byte(s)
Log Message:
Now makes a header row and puts PDF at the end of the file name.

File Contents

# User Rev Content
1 ninoborges 8 """
2     SingleTiffToLFP
3     Created by Emanuel Borges
4     3.22.07
5    
6     This program will take a dir of single page tiffs and create an LPF from the tiffs
7     found there. It will know the doc boundaries because the tiffs are <foo>_<pageno>.tif
8     The program will also create a .dat file from the boundaries too.
9    
10     First it packs it into a sortable hash with a sortable list as it's value, then unpacks
11     this list into the two files.
12    
13     <FileNameSansExt:[File1,File2,File3]
14    
15     Currently it only does one dir, since the dict would get Garbage collected but the
16     dict function could be turned into a class.
17     """
18    
19 nino.borges 441 import os, time, NinoGenTools
20 ninoborges 8
21     def CreateDictionary(contentsOfDir):
22     mainDict = {}
23     for file in contentsOfDir:
24     name = os.path.splitext(file)[0]
25     name = name.split("_Page_")[0]
26     if name in mainDict.keys():
27     filesList = mainDict.get(name)
28     filesList.append(file)
29     mainDict[name] = filesList
30     else:
31     mainDict[name]=[file]
32     return mainDict
33    
34     def AddLfpParent(file, outputOBJ,prefix, curNumb):
35 nino.borges 441 outputOBJ.write("IM,%s,D,0,@%s;;%s;2\n"% (prefix+str(curNumb),time.strftime('%Y%m%d'),file))
36 ninoborges 8
37     def AddLfpChild(file, outputOBJ,prefix, curNumb):
38 nino.borges 441 outputOBJ.write("IM,%s,,0,@%s;;%s;2\n"% (prefix+str(curNumb),time.strftime('%Y%m%d'),file))
39 ninoborges 8
40     def AddToDAT(beg,end, outputOBJ):
41     """This only works if the file is named the bates..."""
42     outputOBJ.write(beg + "," + end + ","+(time.strftime('%Y%m%d')+"\n"))
43    
44     def AddToFile(startDir, file, outputFile):
45     output = open(outputFile, "a")
46     output.write("IM,PP002561,D,1,@%s;;%s;2\n"% (time.strftime('%Y%m%d'),file))
47    
48 nino.borges 443 def Process(startDir,prefix,startNumb,counterBase,extension =".tif"):
49 nino.borges 478 startNumb = startNumb -1
50 ninoborges 8 lfpFileOBJ = open(startDir + "\\new_load.lfp",'w')
51     datFileOBJ = open(startDir + "\\new_load.dat",'w')
52 nino.borges 526 ## Add Headder Row
53     datFileOBJ.write("BegNo|EndNo|LoadDate|FileName\n")
54 ninoborges 8 dirList = []
55 nino.borges 441 counterObject = NinoGenTools.Counter()
56    
57 ninoborges 8 for file in os.listdir(startDir):
58 nino.borges 447 if os.path.splitext(file)[1].upper() == extension.upper():
59 ninoborges 8 dirList.append(file)
60 nino.borges 447 elif os.path.splitext(file)[1].upper() == extension.upper()+"F":
61     dirList.append(file)
62 ninoborges 8 filesMatrix = CreateDictionary(dirList)
63 nino.borges 441 counterObject.inc(startNumb)
64 ninoborges 8 fileMatrixList = filesMatrix.keys()
65     fileMatrixList.sort()
66     for k in fileMatrixList:
67     values = filesMatrix[k]
68 nino.borges 441 datFileOBJ.write(prefix + "%0*d"% (counterBase,counterObject.count))
69     AddLfpParent(values[0], lfpFileOBJ, prefix, "%0*d"% (counterBase,counterObject.count))
70     counterObject.inc(1)
71 ninoborges 8 if len(values) > 1:
72     for value in values[1:]:
73 nino.borges 441 AddLfpChild(value, lfpFileOBJ, prefix, "%0*d"% (counterBase,counterObject.count))
74     counterObject.inc(1)
75 nino.borges 443
76 nino.borges 526 datFileOBJ.write("|" + prefix + "%0*d"% (counterBase,counterObject.count - 1)+"|"+ time.strftime('%Y%m%d') +"|"+ k+".PDF" +"\n")
77 nino.borges 443
78    
79     if __name__ == '__main__':
80     #startDir = r"\\lisdisdss01\dis20\c_d\37771188\20100503"
81 nino.borges 478 startDir = r"T:\honeywell\tst"
82 nino.borges 443 extension = ".tif"
83     #extension = ".jpg"
84     prefix = 'tst-'
85 nino.borges 478 startNumb = 1
86 nino.borges 443 counterBase = 8
87     Process(startDir,prefix,startNumb,counterBase,extension)