-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyPDF2_pdfplumber_multiple_templates.py
64 lines (56 loc) · 2.46 KB
/
PyPDF2_pdfplumber_multiple_templates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import PyPDF2
import sys
import pdfplumber
import win32com.client
import os
filenamelist = ["Amazon music 1112.pdf", "Etsy music 1112.pdf", "Amazon hogwarts 1112.pdf", "Etsy hogwarts 1112.pdf"]
filltext = [[[]],[[]]]
filltextcurrent = [0, 0]
for filename in filenamelist:
templateselection = 0
if "hogwarts" in filename:
templateselection = 1
if "Amazon" in filename:
with pdfplumber.open(filename) as currentfile:
for page in currentfile.pages:
pagetext = page.extract_text()
if len(filltext[templateselection][filltextcurrent[templateselection]]) < 12:
filltext[templateselection][filltextcurrent[templateselection]] += [pagetext[pagetext.find("(#000000)")+len("Font Color: f (#000)"):pagetext.find("Grand total")]]
else:
filltextcurrent[templateselection] += 1
filltext[templateselection].append([])
else:
currentfile = PyPDF2.PdfFileReader(open(filename, 'rb'))
for a in range(0, currentfile.numPages-1):
pagetext = currentfile.getPage(a).extractText()
if len(filltext[templateselection][filltextcurrent[templateselection]]) < 6:
filltext[templateselection][filltextcurrent[templateselection]] += [pagetext[pagetext.find("Personalization:")+len("Personalization:"):pagetext.find("Do the green thing")]]
else:
filltextcurrent[templateselection] += 1
filltext[templateselection].append([])
# Open photoshop
# psApp = win32com.client.Dispatch("Photoshop.Application")
batchcount = 0
for batch in filltext[0]:
# Open template file
# psApp.Open(r"C:\path\to\template.psd")
# doc = psApp.Application.ActiveDocument
# Set text for all layers
for a in range(1, len(batch)):
# doc.ArtLayers[str(a)].TextItem.contents = batch[a-1]
print(batch[a-1])
# Save as a new psd
# doc.SaveAs(r"C:\path\to\batch"+str(batchcount)+".psd")
batchcount += 1
print("")
for batch in filltext[1]:
# Open template file
# psApp.Open(r"C:\path\to\template2.psd")
# doc = psApp.Application.ActiveDocument
# Set text for all layers
for a in range(1, len(batch)):
# doc.ArtLayers[str(a)].TextItem.contents = batch[a-1]
print(batch[a-1])
# Save as a new psd
#doc.SaveAs(r"C:\path\to\batch"+str(batchcount)+".psd")
batchcount += 1