/
convert.py
172 lines (150 loc) · 6.67 KB
/
convert.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
import sys
import os
import re
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LAParams, LTContainer, LTTextBox
from pdfminer.pdfinterp import PDFPageInterpreter, PDFResourceManager
from pdfminer.pdfpage import PDFPage
## edit here!!
gameName = "WorldShort2018"
def find_textboxes_recursively(layout_obj):
"""
再帰的にテキストボックス(LTTextBox)を探して、テキストボックスのリストを取得する。
"""
# LTTextBoxを継承するオブジェクトの場合は1要素のリストを返す。
if isinstance(layout_obj, LTTextBox):
return [layout_obj]
# LTContainerを継承するオブジェクトは子要素を含むので、再帰的に探す。
if isinstance(layout_obj, LTContainer):
boxes = []
for child in layout_obj:
boxes.extend(find_textboxes_recursively(child))
return boxes
return [] # その他の場合は空リストを返す。
def style(str, ir):
if ir == "i":
if str == "Freestyle":
return "FreeStyle"
elif str == "Backstroke":
return "BackStroke"
elif str == "Breaststroke":
return "BreastStroke"
elif str == "Individual":
return "IndividualMedley"
else:
return str
elif ir == "r":
if str == "Freestyle":
return "FreeRelay"
elif str == "Medley":
return "MedleyRelay"
# Layout Analysisのパラメーターを設定。縦書きの検出を有効にする。
laparams = LAParams(detect_vertical=True)
# 共有のリソースを管理するリソースマネージャーを作成。
resource_manager = PDFResourceManager()
# ページを集めるPageAggregatorオブジェクトを作成。
device = PDFPageAggregator(resource_manager, laparams=laparams)
# Interpreterオブジェクトを作成。
interpreter = PDFPageInterpreter(resource_manager, device)
# 出力用のテキストファイル
filename = os.path.basename(sys.argv[1])
outputfilename = "output/text/" + os.path.splitext(filename)[0] + "_out.txt"
outputfile = open(outputfilename, 'w')
output_txt = ""
prefix = ""
allData = ""
ir = "i"
def print_and_write(txt):
print(txt)
#outputfile.write(txt)
# output_txt.write('\n')
outputfile.close()
with open(sys.argv[1], 'rb') as f:
# PDFPage.get_pages()にファイルオブジェクトを指定して、PDFPageオブジェクトを順に取得する。
# 時間がかかるファイルは、キーワード引数pagenosで処理するページ番号(0始まり)のリストを指定するとよい。
rank = False
lanes = []
thisHeat = ""
number = 1
semi = False
seminumber = 1
for page in PDFPage.get_pages(f):
# print_and_write('\n====== ページ区切り ======\n')
interpreter.process_page(page) # ページを処理する。
layout = device.get_result() # LTPageオブジェクトを取得。
# ページ内のテキストボックスのリストを取得する。
boxes = find_textboxes_recursively(layout)
# テキストボックスの左上の座標の順でテキストボックスをソートする。
# y1(Y座標の値)は上に行くほど大きくなるので、正負を反転させている。
boxes.sort(key=lambda b: (-b.y1, b.x0))
for box in boxes:
text = box.get_text()
if prefix != "":
if "Heat" in text and "of" in text:
if "Slow" in text or "Fast" in text:
number = int(text.split(" ")[2])
else:
number = int(text.split(" ")[1])
if rank == True and len(lanes) == 0:
lanes = text.split()
elif rank == True:
names = text.split("\n")
if semi == True:
thisHeat = prefix + "_" + str(seminumber) + "_AgeGroup0_["
seminumber += 1
elif semi == False:
thisHeat = prefix + "_" + str(number) + "_AgeGroup0_["
for i in range(len(lanes)):
name = names[i]
name = name.replace(" - ", "_")
name = name.replace(" ", "_")
name = name.replace("-", "_")
thisHeat += lanes[i] + "#" + name + "-"
thisHeat = "CamXX_" + gameName + "_" + thisHeat[:-1] + "].mp4\n"
#print(thisHeat)
allData += thisHeat
rank = False
lanes = []
elif "Men's" in text and "men" not in output_txt:
if text.count(os.linesep) != 1:
text = text.split("\n")[0]
styles = text.split(" ")
if "x" in styles[1]:
ir = "r"
output_txt += "Men_" + styles[1] + "_" + style(styles[2].split("\n")[0], ir)
ir = "i"
elif "Women's" in text and "women" not in output_txt:
if text.count(os.linesep) != 1:
text = text.split("\n")[0]
styles = text.split(" ")
if "x" in styles[1]:
ir = "r"
output_txt += "Women_" + styles[1] + "_" + style(styles[2].split("\n")[0], ir)
ir = "i"
elif "Mixed" in text and "mixed" not in output_txt:
if text.count(os.linesep) != 1:
text = text.split("\n")[0]
styles = text.split(" ")
if "x" in styles[1]:
ir = "r"
output_txt += "Mixed_" + styles[1] + "_" + style(styles[2].split("\n")[0], ir)
ir = "i"
elif "_" in output_txt and\
("Heat" not in output_txt or\
"Final" not in output_txt or\
"Swim-Off" not in output_txt or\
"Semi" not in output_txt):
output_txt += "_" + text.split("\n")[0]
prefix = output_txt
if "Semi" in prefix:
semi = True
#elif "_" in output_txt and "final" not in output_txt:
# output_txt += "_Final"
# prefix = output_txt
if "Reserves" in text:
break
if "Rank" in text:
rank = True
# print_and_write('-' * 10) # 読みやすいよう区切り線を表示する。
# print_and_write(box.get_text().strip()) # テキストボックス内のテキストを表示する。
print_and_write(allData)