forked from agbilotia1998/slydok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
180 lines (154 loc) · 5.33 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from pptx import Presentation
# from googleapiclient import http
# from googleapiclient.discovery import build
# from httplib2 import Http
# from oauth2client import file, client, tools
from io import BytesIO, FileIO
from docx import Document
from pptx.util import Pt
import docx
from pptx.util import Inches
from images import extract_images
import scipy.ndimage
import re
from flask import Flask, request
import subprocess
import time
app = Flask(__name__)
def get_template_name(arg):
switcher = {
1: 'consulting.pptx',
2: 'pitch.pptx',
3: 'your_big_idea.pptx',
4: 'portfolio.pptx'
}
return switcher.get(arg)
def get_filename(url):
s = url.split('/')
print s[-2]
return s[-2]
@app.route("/")
def go():
filep = request.args.get('filename')
template = request.args.get('templNo')
template = int(template)
filep = filep[1:len(filep) - 1]
print filep
type = request.args.get('flag')
print type
# url = request.args.get('url')
filename = ''
if type == 'file':
filename = filep
else:
cmd = 'python download.py ' + get_filename(filep)
subprocess.call(cmd, shell=True)
filename = 'test.docx'
time.sleep(5)
document = Document(filename)
template = get_template_name(template)
prs = Presentation('templates/' + template)
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
body_shape = None
tf = None
images = extract_images(filename)
image_count = 1
default_constraint = 900
constraint = 900
def add_overflow_slide(result):
layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(layout)
body_shape = slide.shapes.add_textbox(Inches(0.5),Inches(1.15),Inches(8.8),Inches(0.0))
global tf
tf = body_shape.text_frame
tf.word_wrap = True
p = tf.add_paragraph()
p.text = result
p.level = 0
global constraint
constraint = default_constraint - len(result)
print constraint
def emu_to_pixels(emu):
return int(round(emu / 9525.0))
def pixels_to_emu(pixels):
return int(pixels * 9525)
def iter_headings(paragraphs):
for paragraph in paragraphs:
if paragraph.style.name.startswith('Heading'):
yield paragraph
my_list = list()
ct = 0
head = ''
present = 0
for para in document.paragraphs:
my_list.append(para.style.name)
if para.style.name == 'Title':
title.text = para.text
if para.style.name == 'Subtitle':
subtitle.text = para.text
if para.style.name.startswith('Heading'):
if(not my_list[ct-1].startswith('Heading')):
layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(layout)
# slide.placeholders[1].getparent().remove(slide.placeholders[1])
heading = slide.shapes.title
heading.text = para.text
constraint = default_constraint
body_shape = slide.shapes.add_textbox(Inches(0.5),Inches(1.15),Inches(8.8),Inches(0.0))
tf = body_shape.text_frame
tf.word_wrap = True
else:
p = tf.add_paragraph()
p.font.bold = True
p.font.size = Pt(16)
#head += para.text + '\n'
p.text = para.text
if(para.style.name == 'Normal' or para.style.name == 'normal'):
# run = text_place.add_run()
# font = run.font
# font.size = Pt(18)
# slide_list = get_slides(para.text, present)
# if len(slide_list) == 1:
# present += len(slide_list[-1])
# else:
# present = len(slide_list[-1])
# print present
# if(len(arr) > 2):
# result = gensim_summarizer(para.text)
# else:
result = head + para.text
print "-----" + str(len(result))
print "--------" + str(constraint)
if len(result) > constraint:
head = ''
p = tf.add_paragraph()
p.text = result[:constraint+1]
p.level = 0
add_overflow_slide(result[constraint + 1:])
else:
constraint -= len(result)
head = ''
p = tf.add_paragraph()
p.bullet = "*"
p.text = result
p.level = 0
# for i in range(1, len(slide_list)):
# add_overflow_slide(slide_list[i])
if len(para._p.r_lst[0].drawing_lst):
image_name = 'image{0}.jpeg'.format(image_count)
image_count += 1
height, width, channels = scipy.ndimage.imread(image_name).shape
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = (prs.slide_width - pixels_to_emu(width)) / 3
top = (prs.slide_height - pixels_to_emu(height)) / 3
pic = slide.shapes.add_picture(image_name, left, top)
ct+=1
print para.style
print(title.text, subtitle.text)
prs.save('../test.pptx')
return 'done'
app.run(debug=True)