-
Notifications
You must be signed in to change notification settings - Fork 0
/
docx2json.py
55 lines (46 loc) · 1.91 KB
/
docx2json.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
"""
@author: Varun
"""
import mammoth
import os
from bs4 import BeautifulSoup
import json
import re
#Folder path
path = "D:\\Work\\RnR\\soho_updated\\SOHO_in_R_R - Copy\\SOHO_new\\New folder\\international"
#Extract document file names
docfileNames = os.listdir(path+"\\docFiles")
#method to convert split document into multiple HTML Files and convert to JSON
def docx2json(fileName):
with open(path + "\\docFiles\\" + fileName + ".docx", "rb") as docx_file:
#convert to HTML
result = mammoth.convert_to_html(docx_file)
html = result.value # The generated HTML
#split each html by heading
htmlsplit = html.split("<h1>")
htmlsplit = ["<h1>" + html for html in htmlsplit]
for unit in htmlsplit[1:]:
soup = BeautifulSoup(unit, 'html.parser')
title = soup.h1.string
data = {}
#extract h1 text for file name
cleanh1 = re.sub('[^0-9a-zA-Z]+', ' ', str(soup.h1.string))
data['title'] = title
htmlPath = path + "\\htmlFiles\\" + cleanh1 + ".html"
with open(htmlPath, "w") as html:
html.write(str(soup))
print("Converstion of " + fileName + ".docx completed")
#remove h1 from json
for tag in soup.find_all('h1'):
tag.replaceWith('')
data['body'] = str(soup)
json_data = json.dumps(data)
print("Converstion of JSON for " + fileName + ".docx started")
with open(path+"\\jsonFiles\\"+cleanh1+".json", "w") as file:
file.write(json_data)
print("Converstion of JSON for " + fileName + ".docx completed")
for fileName in docfileNames:
fileName = fileName[0:-5]
print(".docx to HTML files Converstion in Progress: " + fileName + ".docx")
#call method for each file
docx2json(fileName)