-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
156 lines (136 loc) · 5.2 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import docx
import sys
try:
user_arg = sys.argv[1]
except Exception as e:
print("Please Enter the file name to be edited as argument... (Only Docx Format)")
sys.exit()
print("Enter all details to be replaced...")
usr1 = input("Exact Name written in provided file: ")
usr2 = input("Exact Enroll written in provided file: ")
usr3 = input("Exact Division written in provided file: ")
main_list = []
#Get all style values
def getvalue(pH):
bold = pH.bold
italic = pH.italic
fontsize = pH.font.size
fontcolor = pH.font.color.rgb
return [bold,italic,fontsize,fontcolor]
#Set all style values
def addvalue(run,list1):
run.bold = list1[0]
run.italic = list1[1]
run.font.size = list1[2]
run.font.color.rgb = list1[3]
return run
def fixruns(run,para,oldname,newname):
oldlist = oldname.split()
newlist = newname.split()
for run in para.runs:
try:
if(run.text.find(oldlist[0]) != -1):
try:
run_vals = getvalue(run)
run.text = run.text.replace(oldlist[0],newlist[0])
run = addvalue(run,run_vals)
except:
run.text = ""
elif(run.text.find(oldlist[1]) != -1):
try:
run_vals = getvalue(run)
run.text = run.text.replace(oldlist[1],newlist[1])
run = addvalue(run,run_vals)
except:
run.text = ""
elif(run.text.find(oldlist[2]) != -1):
try:
run_vals = getvalue(run)
run.text = run.text.replace(oldlist[2],newlist[2])
run = addvalue(run,run_vals)
except:
run.text = ""
except:
continue
while(True):
usr_list = []
for i in range(0,3):
if(i == 0):
inp = input("Enter New Name: ")
elif(i == 1):
inp = input("Enter New Enroll: ")
elif(i == 2):
inp = input("Enter New Division: ")
usr_list.append(inp)
main_list.append(usr_list)
inp = input("Enter to Add More (press e and enter to exit the adding process!): ")
if(inp == "e"):
break
else:
continue
subject = input("State Subject Name: ")
pracassign = input("State Practicals or Assignemnts in format(Practicals(1-7) or Assignments(1-2)): ")
print("Creating Lists of Profiles...")
print(main_list)
for user in main_list:
#Managing Header Sections
print("\n\n Changing Header portion..(if required)...")
doc = docx.Document(user_arg)
section = doc.sections[0]
header = section.header
for para in header.paragraphs:
for run in para.runs:
if(run.text.find(usr2) != -1):
run_vals = getvalue(run)
run.text = run.text.replace(usr2,user[1])
run = addvalue(run,run_vals)
else:
pass
#Managing Data Entires
print("\n\n Changing file Data....")
for para in doc.paragraphs:
for run in para.runs:
if(run.text.find(usr1) != -1):
run_vals = getvalue(run)
run.text = run.text.replace(usr1,user[0])
run = addvalue(run,run_vals)
else:
if(para.text.find(usr1) != -1):
fixruns(run,para,usr1,user[0])
else:
pass
if(run.text.find(usr2) != -1):
run_vals = getvalue(run)
run.text = run.text.replace(usr2,user[1])
run = addvalue(run,run_vals)
if(run.text.find(usr3) != -1):
run_vals = getvalue(run)
run.text = run.text.replace(usr3,user[2])
run = addvalue(run,run_vals)
#Working with tables..
print("\n\n Changing data in tables...(if required)...")
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
for para in cell.paragraphs:
for run in para.runs:
if(run.text.find(usr1) != -1):
run_vals = getvalue(run)
run.text = run.text.replace(usr1,user[0])
run = addvalue(run,run_vals)
else:
if(para.text.find(usr1) != -1):
fixruns(run,para,usr1,user[0])
else:
pass
if(run.text.find(usr2) != -1):
run_vals = getvalue(run)
run.text = run.text.replace(usr2,user[1])
run = addvalue(run,run_vals)
if(run.text.find(usr3) != -1):
run_vals = getvalue(run)
run.text = run.text.replace(usr3,user[2])
run = addvalue(run,run_vals)
print("Creating files...")
name = f"{user[1]}_{subject}_{pracassign}.docx"
doc.save(name)