-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOCXHandler.py
65 lines (44 loc) · 2.15 KB
/
DOCXHandler.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
import mammoth
import os
import sys
import shutil
class DOCXHandler:
def __init__(self, docx_dir):
self.docx_dir = docx_dir
self.image_counter = 0
self.image_dir = None
self.target_dir = None
def extract(self, target_dir):
self.target_dir = target_dir
files = os.listdir(self.docx_dir)
files = [os.path.join(self.docx_dir, f) for f in files if f.endswith(".docx")]
#files.sort(key = lambda x: os.path.getmtime(x))
#files.reverse()
for i, docx_filename in enumerate(sorted(files)):
print(docx_filename)
self.image_dir = "blog" + str(i) + "_images"
if not os.path.exists(os.path.join(target_dir, self.image_dir)):
os.mkdir(os.path.join(target_dir, self.image_dir))
html_name = "blog" + str(i) + "-" + str(docx_filename.split("/")[-1]).replace(".docx", ".html")
with open(target_dir + "/" + html_name, 'w') as html_file:
with open(docx_filename, "rb") as docx_file:
result = mammoth.convert_to_html(docx_file, convert_image=mammoth.images.img_element(self.convert_image))
html = result.value # The generated HTML
messages = result.messages # Any messages, such as warnings during conversion
if messages:
print("Parsing Message: " + str(messages))
html_file.write(html)
def convert_image(self, image):
with image.open() as image_bytes:
image_filename = "image" + str(self.image_counter) + "." + str(image.content_type.split("/")[-1])
self.image_counter += 1
full_image_filename = os.path.join(self.target_dir, self.image_dir, image_filename)
with open(full_image_filename, 'wb') as img:
img.write(image_bytes.read())
# encoded_src = base64.b64encode(image_bytes.read()).decode("ascii")
#image.
#image.
return {
#"src": "data:{0};base64,{1}".format(image.content_type, encoded_src)
"src" : os.path.join(self.image_dir, image_filename)
}