-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpptx2figs.py
More file actions
193 lines (156 loc) · 6.67 KB
/
pptx2figs.py
File metadata and controls
193 lines (156 loc) · 6.67 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
from genericpath import isfile
import os
import shutil
import sys
import getopt
try:
from pptx import Presentation
except:
print("you have no python-pptx!")
try:
import win32com.client
except:
print("you have no pywin32!")
usage = '''pptx2figs.py usage:
python pptx2figs.py <options>
--input=<str> / -i : 対象のpptxファイルネーム
--start=<number> / -s : 探索開始のスライド番号 (default : 0)
--end=<number> / -e : 探索終了のスライド番号 (default : None)
'''
# 保存先のフォルダ、上書きしてしまうので注意
f_pptx = "./pptxs/"
f_pdf = "./pdfs/"
f_png = "./pngs/"
''' 入力pptxファイルから対象図のみのpptxファイルを作成する '''
def make_1fig_pptx(prs, pptx_filename, t_page, t_shape_id):
# shapeオブジェクトをコピーして代入が上手く動かないので、prsを削る方向で進める
# dst = copy.deepcopy(prs)だと上手く動かなかったので、一旦別ファイルとして保存する
prs.save(pptx_filename)
dst = Presentation(pptx_filename)
# 情報取得
t_shape = dst.slides[t_page].shapes[t_shape_id]
bias_x = t_shape.left
bias_y = t_shape.top
width = t_shape.width
height = t_shape.height
# スライドサイズ変更
dst.slide_width = width
dst.slide_height = height
# t_page以外は削除
xml_slides = dst.slides._sldIdLst
slides = list(xml_slides)
for page in range(len(dst.slides) - 1, -1, -1):
if page != t_page:
rId = dst.slides._sldIdLst[page].rId
dst.part.drop_rel(rId)
xml_slides.remove(slides[page])
# (枠+pdf_name)shapeを削除する
shapes = dst.slides[0].shapes # target slideは0ページになっている
shapes.element.remove(shapes[t_shape_id].element)
# 全てのオブジェクトの原点を変更
for shape in shapes:
shape.left -= bias_x
shape.top -= bias_y
# pptx_filenameに上書き保存
dst.save(pptx_filename)
''' pptxファイルからpdfファイルを作成する '''
def make_1fig_pdf(pptx_filename, pdf_filename):
# 参考:https://python-work.com/pptx-to-pdf/
# 絶対パスで設定しないと動かない
pptx_filename_abs = os.path.join(os.path.abspath("./"), pptx_filename)
pdf_filename_abs = os.path.join(os.path.abspath("./"), pdf_filename)
# Powerpointファイルを開きPDF形式で保存
application = win32com.client.Dispatch('Powerpoint.Application')
presentation = application.Presentations.Open(pptx_filename_abs, WithWindow=False)
presentation.SaveAs(pdf_filename_abs, 32)
# アプリケーション終了処理
presentation.close()
application.quit()
presentation = None
application = None
''' pptxファイルからpngファイルを作成する '''
def make_1fig_png(pptx_filename, png_filename):
# # 絶対パスで設定しないと動かない
pptx_filename_abs = os.path.join(os.path.abspath("./"), pptx_filename)
png_filename_abs = os.path.join(os.path.abspath("./"), png_filename)
# Powerpointファイルを開きPDF形式で保存
application = win32com.client.Dispatch('Powerpoint.Application')
presentation = application.Presentations.Open(pptx_filename_abs, WithWindow=False)
presentation.Export(png_filename_abs, FilterName="png")
# HOGEHOGE.pngを保存したい場合、HOGEHOGE/スライド1.PNGになるので、修正する
shutil.move(png_filename[:-4] + "/スライド1.PNG", png_filename)
os.rmdir(png_filename[:-4])
# アプリケーション終了処理
presentation.close()
application.quit()
presentation = None
application = None
''' 入力pptxファイルから図にしたいとこを全て抽出してpptx, pdf, pngを作成する '''
def make_figs(target_filename, start_page = 0, end_page = None):
# 保存先のフォルダを作っておく
if not os.path.exists(f_pptx):
os.makedirs(f_pptx)
if not os.path.exists(f_pdf):
os.makedirs(f_pdf)
if not os.path.exists(f_png):
os.makedirs(f_png)
# target_filename (.pptx)を開く
if target_filename[-5:] != ".pptx":
print(f"{target_filename} is not a pptx file!")
return
if not os.path.isfile(target_filename):
print(f"{target_filename} is not found!")
return
prs = Presentation(target_filename)
# textが*.pdfのshapeがあれば、pptx, pdfを作成する
print("[Targets]")
for page, slide in enumerate(prs.slides):
if page < start_page or (end_page != None and page > end_page):
continue
for shape_id, shape in enumerate(slide.shapes):
try:
if '.pdf' in shape.text: # 四角図形の場合も加えたい
print(f" - slide {page} : {shape.text}")
pptx_filename = f_pptx + shape.text[:-4] + ".pptx"
pdf_filename = f_pdf + shape.text
png_filename = f_png + shape.text[:-4] + ".png"
# 対象figureだけのpptxを作成
make_1fig_pptx(prs, pptx_filename, page, shape_id)
# 対象figureだけのpptxからpdfを作成
make_1fig_pdf(pptx_filename, pdf_filename)
# 対象figureだけのpptxからpngを作成
make_1fig_png(pptx_filename, png_filename)
except:
pass
if __name__ == "__main__":
start_page = 0
end_page = None
input_filename = None
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, 'h:i:s:e', ['help', 'input=', 'start=', 'end='])
except getopt.GetoptError:
print(usage)
sys.exit()
for opt, arg in opts:
try:
if opt in ('-h', '--help'):
print(usage)
sys.exit()
elif opt in ('-i', '--input'):
input_filename = arg
elif opt in ('-s', '--start'):
start_page = int(arg)
elif opt in ('-e', '--end'):
end_page = int(arg)
except Exception:
print('Error parsing argument: %s' % opt)
print(usage)
sys.exit(2)
if input_filename is not None:
make_figs(input_filename, start_page, end_page)
else:
f_input = "./input_pptxs/"
files = [f_input + f for f in os.listdir(f_input) if '.pptx' in f]
for file in files:
make_figs(file, start_page, end_page)