-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangeInCodeHTML.py
48 lines (42 loc) · 1.53 KB
/
ChangeInCodeHTML.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
import shutil
import os
import re
## Script will change pagenumbers inside the HTML to the filename found.
folder = "C:\wamp\www\parker\working\Text\edit\do"
os.chdir(folder)
startingpage = 0
def changenumbers(folder):
for afile in os.listdir(folder):
if afile.endswith('.xhtml'):
page = re.search('page([0-9]*)\.xhtml', afile)
if int(page.group(1)) > startingpage:
shutil.move( afile, afile + ".bak" )
destination = open( afile, "w" )
source = open( afile + ".bak", "r" )
if page:
print "Processing Page" + page.group(1)
for line in source:
m = re.search(r'<div class="pagenumber">([0-9]*)</div>', line)
nm = re.search(r'<img alt="" src="\.\./Images/page([0-9]*)\.jpg" />', line)
if m is not None:
#print "Found in page " + page.group(1) + ", text showing " + m.group(1)
#newpage = m.sub('',line)
line = re.sub(r'<div class="pagenumber">([0-9]*)</div>', '<div class="pagenumber">'+page.group(1)+'</div>', line)
print line
destination.write(line)
elif nm is not None:
line = re.sub(r'<img alt="" src="\.\./Images/page([0-9]*)\.jpg" />', '<img alt="" src="../Images/page'+page.group(1)+'.jpg" />', line)
print line
destination.write(line)
else:
destination.write(line)
source.close()
destination.close()
def restorebaks(folder):
for afile in os.listdir(folder):
if afile.endswith('.xhtml'):
os.remove(afile)
elif afile.endswith('.bak'):
shutil.move( afile, afile[:-4] )
changenumbers(folder)
#restorebaks(folder)