-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter.py
488 lines (375 loc) · 16 KB
/
exporter.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import os
import re
import tempfile
from bs4 import BeautifulSoup, Tag
from ebooklib import epub
from odf import style
from odf import text as odftext
from odf.draw import Frame, Image, TextBox
from odf.opendocument import OpenDocumentText
from odf.style import (
Footer,
GraphicProperties,
Header,
MasterPage,
PageLayout,
PageLayoutProperties,
ParagraphProperties,
Style,
TextProperties,
)
from odf.text import A, LineBreak, P, PageNumber, Span
from papersize import SIZES, parse_length
from PySide6 import QtGui, QtWidgets, QtCore
from box_editor.box_data import BOX_DATA_TYPE, BoxData
from document_helper import DocumentHelper
from project import Page, Project
class Exporter:
def __init__(self, parent: QtWidgets.QWidget):
self.parent = parent
def open(self, temp_dir: tempfile.TemporaryDirectory, project: Project) -> bool:
self.temp_dir = temp_dir
self.project = project
self.name = project.name
self.author = ""
self.current_page = None
self.current_page_nr = 0
return True
def finish(self):
pass
def open_finished_dialog(self, filename: str):
response = QtWidgets.QMessageBox.question(
self.parent,
"Export Successful",
f"File {filename} exported successfully.\nDo you want to open it?",
QtWidgets.QMessageBox.StandardButton.Ok
| QtWidgets.QMessageBox.StandardButton.Cancel,
QtWidgets.QMessageBox.StandardButton.Ok,
)
if response == QtWidgets.QMessageBox.StandardButton.Open:
# open the file using the associated application
os.system(f"xdg-open {filename}")
def write_box(self, box_data: BoxData):
pass
def new_page(self, page: Page, page_nr: int):
self.current_page = page
self.current_page_nr = page_nr
self.current_image = QtGui.QPixmap(self.current_page.image_path)
def prepare_filename(self, filename, extension) -> str:
if os.path.splitext(filename)[1] != "." + extension:
filename += "." + extension
return filename
class ExporterODT(Exporter):
def __init__(self, parent: QtWidgets.QWidget):
super().__init__(parent)
def open(self, temp_dir: tempfile.TemporaryDirectory, project: Project) -> bool:
super().open(temp_dir, project)
self.odf_text = OpenDocumentText()
self.frame_style = Style(name="FrameStyle", family="graphic")
self.frame_style.addElement(
GraphicProperties(
verticalpos="from-top",
verticalrel="page",
horizontalpos="from-left",
horizontalrel="page",
stroke="none",
fill="none",
)
)
self.odf_text.automaticstyles.addElement(self.frame_style)
self.main_p = P()
self.odf_text.text.addElement(self.main_p)
return True
def write_box(self, box_data: BoxData):
box_id = "box" + str(self.current_page_nr) + "_" + str(box_data.order)
if self.current_page:
unit = "in"
x = str(box_data.rect.x() / self.current_page.ppi) + unit
y = str(box_data.rect.y() / self.current_page.ppi) + unit
width = str(box_data.rect.width() / self.current_page.ppi) + unit
heigth = str(box_data.rect.height() / self.current_page.ppi) + unit
frame = Frame(
width=width,
height=heigth,
x=x,
y=y,
anchortype="page",
stylename=self.frame_style,
)
match box_data.type:
case BOX_DATA_TYPE.TEXT:
text_box = TextBox()
box_style = Style(name=box_id, family="paragraph")
box_style.addElement(
TextProperties(
attributes={
"fontsize": str(
box_data.ocr_result_block.get_font_size()
)
+ "pt"
}
)
)
self.odf_text.automaticstyles.addElement(box_style)
# document_helper = DocumentHelper(box_data.text.clone(), box_data.language.pt1)
# paragraphs = document_helper.break_document_into_fragments()
paragraphs = box_data.get_paragraphs()
for p, paragraph in enumerate(paragraphs):
p = P(stylename=box_style)
text_box.addElement(p)
# for l, line in enumerate(paragraph):
for f, fragment in enumerate(paragraph):
# format: QtGui.QTextCharFormat = fragment.charFormat()
if "\u2028" in fragment.text():
# Split at line break character
fragment_lines = fragment.text().split("\u2028")
for fl, fragment_line in enumerate(fragment_lines):
p.addText(fragment_line)
if fl < len(fragment_lines) - 1:
p.addElement(LineBreak())
else:
p.addText(fragment.text())
frame.addElement(text_box)
case BOX_DATA_TYPE.IMAGE:
image_path = self.temp_dir.name + "/" + box_id + ".png"
self.current_image.copy(box_data.rect).save(image_path)
frame.addElement(Image(href=self.odf_text.addPicture(image_path)))
self.main_p.addElement(frame)
def new_page(self, page: Page, page_nr: int):
super().new_page(page, page_nr)
# Set page size
pl = PageLayout(name="pagelayout")
pl.addElement(
PageLayoutProperties(
pagewidth=str(
parse_length(SIZES[page.paper_size].split(" x ")[0], "in")
)
+ "in",
pageheight=str(
parse_length(SIZES[page.paper_size].split(" x ")[1], "in")
)
+ "in",
margin="0pt",
)
)
self.odf_text.automaticstyles.addElement(pl)
mp = MasterPage(name="Standard", pagelayoutname=pl)
self.odf_text.masterstyles.addElement(mp)
def finish(self):
# TODO: Generalize file save dialog in base class
# preview = ExporterPreviewWindow(self.parent, document, self.preview_document_changed)
# if preview.exec():
extension = "odt"
filename = QtWidgets.QFileDialog.getSaveFileName(
self.parent,
caption=QtCore.QCoreApplication.translate("ExporterODT", "Export to ODT"),
filter=QtCore.QCoreApplication.translate("ExporterODT", "ODT file (*.odt)"),
)[0]
if filename:
file_extension = os.path.splitext(filename)[1]
if file_extension != "." + extension:
filename += "." + extension
self.odf_text.save(filename)
self.open_finished_dialog(filename)
def preview_document_changed(self, document: QtGui.QTextDocument):
self.document = document
class ExporterPlainText(Exporter):
def __init__(self, parent: QtWidgets.QWidget):
super().__init__(parent)
def open(self, temp_dir: tempfile.TemporaryDirectory, project: Project) -> bool:
super().open(temp_dir, project)
return True
def write_box(self, box_data: BoxData, page: Page, page_nr: int):
if box_data.type is BOX_DATA_TYPE.TEXT:
self.text += box_data.text.toPlainText() + "\n"
def new_page(self, page: Page, page_nr: int):
super().new_page(page, page_nr)
if page_nr > 1:
self.text += "\n\n"
def finish(self):
document = QtGui.QTextDocument(self.text)
preview = ExporterPreviewWindow(
self.parent, document, self.preview_document_changed
)
if preview.exec():
extension = "txt"
filename = QtWidgets.QFileDialog.getSaveFileName(
self.parent,
caption=QtCore.QCoreApplication.translate(
"dialog_export_caption_plain_text", "Export to Plain Text"
),
filter=QtCore.QCoreApplication.translate(
"dialog_export_filter_plain_text", "Text file (*.txt)"
),
)[0]
self.text = ""
file_extension = os.path.splitext(filename)[1]
if file_extension != "." + extension:
filename += "." + extension
with open(self.prepare_filename(filename, extension), "w") as file:
file.write(self.text)
self.open_finished_dialog(filename)
def preview_document_changed(self, document: QtGui.QTextDocument):
self.document = document
class ExporterPreviewWindow(QtWidgets.QDialog):
def __init__(self, parent, document: QtGui.QTextDocument, callback):
super().__init__(parent)
self.callback = callback
self.resize(1000, 800)
layout = QtWidgets.QGridLayout(self)
self.setLayout(layout)
self.preview = QtWidgets.QTextEdit(self)
self.preview.textChanged.connect(self.preview_edited)
self.preview.setDocument(document)
layout.addWidget(self.preview, 0, 0)
self.setWindowTitle(
QtCore.QCoreApplication.translate(
"dialog_export_window_title_preview", "Export Preview"
)
)
buttons = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.StandardButton.Ok
| QtWidgets.QDialogButtonBox.StandardButton.Cancel
)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
def preview_edited(self):
self.callback(self.preview.document())
class ExporterEPUB_OptionWindow(QtWidgets.QDialog):
def __init__(self, parent, callback):
super().__init__(parent)
self.callback = callback
layout = QtWidgets.QGridLayout(self)
self.setLayout(layout)
self.css_edit = QtWidgets.QPlainTextEdit(self)
self.css_edit.textChanged.connect(self.css_edited)
self.css_edit.setPlaceholderText(
"Enter CSS code to include in exported EPUB file here."
)
layout.addWidget(self.css_edit, 0, 0)
self.setWindowTitle(
QtCore.QCoreApplication.translate(
"dialog_export_window_title_epub_options", "EPUB Export Options"
)
)
buttons = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.StandardButton.Ok
| QtWidgets.QDialogButtonBox.StandardButton.Cancel
)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
def css_edited(self):
self.callback(self.css_edit.toPlainText())
class ExporterEPUB(Exporter):
def __init__(self, parent: QtWidgets.QWidget):
super().__init__(parent)
self.book = epub.EpubBook()
self.current_chapter = epub.EpubHtml(
title="Intro", file_name="chap_01.xhtml", lang="de"
)
self.current_chapter.content = ""
self.css = ""
self.preview_document = QtGui.QTextDocument()
def open(self, temp_dir: tempfile.TemporaryDirectory, project: Project) -> bool:
super().open(temp_dir, project)
options = ExporterEPUB_OptionWindow(self.parent, self.update_css)
if options.exec():
# with open('resources/epub/nav.css', 'r') as file:
# style = file.read()
if self.css:
nav_css = epub.EpubItem(
uid="style_nav",
file_name="style/nav.css",
media_type="text/css",
content=str.encode(self.css),
)
self.book.add_item(nav_css)
self.current_chapter.add_item(nav_css)
return True
else:
return False
def write_box(self, box_data: BoxData, page: Page, page_nr: int):
text = ""
classes = ""
if box_data.type is BOX_DATA_TYPE.TEXT:
document_helper = DocumentHelper(
box_data.text.clone(), box_data.language.pt1
)
paragraphs = document_helper.break_document_into_fragments()
if not box_data.tag:
box_data.tag = "div"
if box_data.class_:
classes = f' class="{box_data.class_}"'
text += f"<{box_data.tag + classes}>"
for p, paragraph in enumerate(paragraphs):
for l, line in enumerate(paragraph):
for f, fragment in enumerate(line):
format: QtGui.QTextCharFormat = fragment.charFormat()
text += fragment.text()
if box_data.tag:
text += f"</{box_data.tag}>"
self.current_chapter.content = (
str(self.current_chapter.content) + text + "\n\n"
)
elif box_data.type is BOX_DATA_TYPE.IMAGE:
image_format = "JPEG"
image_uid = f"page_{page_nr}_{box_data.order}.{image_format}"
image_path = self.temp_dir.name + "/" + image_uid
QtGui.QPixmap(page.image_path).copy(box_data.rect).save(
image_path, image_format
)
image = epub.EpubImage()
image_content = open(image_path, "rb").read()
image.uid = image_uid
image.file_name = "images/" + image_uid
image.media_type = "image/jpeg"
image.content = image_content
self.book.add_item(image)
# TODO: Calculate height based on ppi
self.current_chapter.content = (
str(self.current_chapter.content)
+ f"""<figure><img style="height: {box_data.rect.height() / 15}em" src="{image.file_name}" alt="{image.file_name}"/>"""
)
self.current_chapter.content = (
str(self.current_chapter.content) + "</figure>\n\n"
)
def finish(self):
self.book.add_item(self.current_chapter)
self.book.add_item(epub.EpubNcx())
self.book.add_item(epub.EpubNav())
self.book.spine = ["nav", self.current_chapter]
cursor = QtGui.QTextCursor(self.preview_document)
cursor.insertHtml(str(self.current_chapter.content))
preview = ExporterPreviewWindow(
self.parent, self.preview_document, self.preview_document_changed
)
if preview.exec():
extension = "epub"
filename = QtWidgets.QFileDialog.getSaveFileName(
self.parent,
caption=QtCore.QCoreApplication.translate(
"dialog_export_caption_epub", "Export to EPUB"
),
filter=QtCore.QCoreApplication.translate(
"dialog_export_filter_epub", "EPUB file (*.epub)"
),
)[0]
self.text = ""
file_extension = os.path.splitext(filename)[1]
if file_extension != "." + extension:
filename += "." + extension
epub.write_epub(self.prepare_filename(filename, extension), self.book, {})
self.open_finished_dialog(filename)
def update_css(self, css: str):
self.css = css
def preview_document_changed(self, document: QtGui.QTextDocument):
self.preview_document = document
class ExporterManager:
def __init__(self):
self.exporters = {}
def add_exporter(self, id, exporter: Exporter):
self.exporters[id] = exporter
def get_exporter(self, id) -> Exporter:
return self.exporters[id]