-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
30 lines (27 loc) · 1.02 KB
/
converter.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
from docx import Document
import io
import shutil
import os
import re
def convertDocxToText(path):
for d in os.listdir(path):
fullText = ""
fileExtension = d.split(".")[-1]
if fileExtension == "docx":
docxFilename = path + d
document = Document(docxFilename)
textFilename = path + d.split(".")[0] + ".txt"
with io.open(textFilename, "w", encoding="utf-8") as textFile:
for para in document.paragraphs:
fullText += " " + para.text
for table in document.tables:
for row in table.rows:
for cell in row.cells:
fullText += " " + cell.text
noSpaces = re.sub(' +', ' ', fullText)
noNewline = re.sub('\n', ' ', noSpaces)
noTabs = re.sub('\t+', ' ', noNewline)
textFile.write(noTabs)
print(docxFilename)
path = "/home/dev/team_blue/python/dlvs_random/"
convertDocxToText(path)