-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathWordParser.py
52 lines (43 loc) · 1.1 KB
/
WordParser.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
import docx
d = docx.Document("demo.docx")
print(d.paragraphs)
print("\n")
print(d.paragraphs[0])
print("\n")
print(d.paragraphs[0].text)
print("\n")
print(d.paragraphs[1].text)
print("\n")
p = d.paragraphs[1]
print(p.runs) # segment of text with a particular format
print(p.runs[0].text)
print(p.runs[1].text)
print(p.runs[2].text)
print(p.runs[3].text)
print(p.runs[4].text)
print(p.runs[2].bold)
print(p.runs[4].italic)
p.runs[4].underline = True
p.runs[4].text = "italic and underlined."
d.save("demo2.docx")
# changing style
p.style = "Title"
d.save("demo3.docx")
# creating a new document
d = docx.Document()
d.add_paragraph("Hello this is a paragraph.")
d.add_paragraph("This is another paragraph.")
d.save("demo4.docx")
p = d.paragraphs[0]
p.add_run("This is a new run.")
print(p.runs)
p.runs[1].bold = True
d.save("demo5.docx")
# all the text in word document as a string
def getText(filename):
doc = docx.Document(filename)
fullText = []
for para in doc.paragraphs:
fullText.append(para.text) # list
return "\n".join(fullText) # string
print(getText("demo.docx")) # pass the filename