TheDarkLord69696969 commited on
Commit
a872010
1 Parent(s): b6ca154

Upload fill_doc.py

Browse files
Files changed (1) hide show
  1. fill_doc.py +50 -0
fill_doc.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from docx import Document
2
+ import os
3
+ import sys
4
+
5
+ def fill_doc(file_path, result_path, variables):
6
+ template_document = Document(file_path)
7
+
8
+ for variable_key, variable_value in variables.items():
9
+ for paragraph in template_document.paragraphs:
10
+ replace_text_in_paragraph(paragraph, variable_key, variable_value)
11
+
12
+ for table in template_document.tables:
13
+ for col in table.columns:
14
+ for cell in col.cells:
15
+ for paragraph in cell.paragraphs:
16
+ replace_text_in_paragraph(paragraph, variable_key, variable_value)
17
+
18
+ template_document.save(result_path)
19
+
20
+
21
+ def replace_text_in_paragraph(paragraph, key, value):
22
+ if key in paragraph.text:
23
+ inline = paragraph.runs
24
+ for item in inline:
25
+ if key in item.text:
26
+ item.text = item.text.replace(key, value)
27
+
28
+ # {% comment %} '''
29
+ # file_path = 'Copy of Last Will and Testament.docx'
30
+ # result_path = 'result.docx'
31
+
32
+ # variables = {
33
+ # "{{What is your name?}}": "",
34
+
35
+ # } {% endcomment %}
36
+
37
+ sys.modules[__name__] = fill_doc
38
+ #os.chdir(r"C:\Users\Prince Raj\Desktop\BOT/")
39
+ # file_path = r"C:\Users\Prince Raj\Desktop\BOT\Last Will and Testament.docx"
40
+ # assert os.path.isfile(file_path)
41
+ # with open(file_path, "r") as f:
42
+ # pass
43
+ #file_path = r"Last Will and Testament.docx"
44
+ # result_path = r"C:\Users\Prince Raj\Desktop\BOT"
45
+ #assert os.path.isfile(result_path)
46
+ #with open(result_path, "r") as f:
47
+ #pass
48
+ # {% comment %} fill_doc(file_path, result_path, variables)
49
+ # '''
50
+ # variables = {} {% endcomment %}