~dricottone/fmg-timesheets

ref: cab4c59713aeea180456717c05ba8f8d42e0597b fmg-timesheets/parser/xml.py -rw-r--r-- 26.6 KiB
cab4c597Dominic Ricottone Data pipeline error 2 years ago
                                                                                
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#!/usr/bin/env python3

import sys
from xml.sax import handler, make_parser
import csv

def printf(string, *variables):
    """Print to STDERR with formatting."""
    sys.stderr.write(string.format(*variables))
    sys.stderr.write("\n")

def is_approximately(location, target):
    """Tests if a location is close enough to a target to be considered equal.

    PDFs store the rendered location of a textbox, not the mathematically-
    ideal location. The net effect is that, while you can rely on the y
    dimension to identify a row, you cannot rely on the x dimension to
    identify a column. My solution is to make equivalence a bit fuzzy, to the
    effect of +/- 5 pixels.
    """
    if (location[1] == target[1]
        and target[0]-5 <= location[0] <= target[0]+5):
        return True
    return False

class TimeSheetHandler(handler.ContentHandler):
    def __init__(self):
        handler.ContentHandler.__init__(self)

        self.text_buffer = ""
        self.line_buffer = []
        self.page_buffer = []

        self.pagenum = None
        self.daterange = None

        self.in_textbox = False
        self.in_text = False
        self.in_figure = False
        self.in_hours_distribution = False

        self.in_header_footer_parts = {
            "timesheet_label": False,
            "timesheet_value": False,
            "location_label": False,
            "location_value": False,
            "department_label": False,
            "department_value": False,
            "employee_type_label": False,
            "employee_type_value": False,
            "location_default_label": False,
            "location_default_value": False,
            "function_label": False,
            "function_value": False,
            "exempt_label": False,
            "exempt_value": False,
            "status_label": False,
            "status_value": False,
            "post_status_label": False,
            "post_status_value": False,
            "validation_label": False,
            "validation_value": False,
            "datetime_label": False,
            "datetime_value": False,
            "total_timesheet_label": False,
            "total_timesheet_value": False,
            "standard_hours_label": False,
            "standard_hours_value": False,
            "total_billable_label": False,
            "total_billable_value": False,
            "percent_billability_label": False,
            "percent_billability_value": False,
            "id_label": False,
            "time_code_label": False,
            "project_label": False,
            "timetype_label": False,
            "day_label": False,
            "total_label": False,
            "_doc_no_label": False,
            "_doc_no_value": False,
            "footer_datetime_value": False,
            "footer_timezone_value": False,
            "footer_pagenum_value": False,
            "footer_pagecount_value": False,
        }

    def startElement(self, name, attrs):
        if name=="page":
            self.in_figure = False
            self.pagenum = attrs["id"]
        elif name=="figure":
            self.in_figure = True
        elif not self.in_figure and not self.in_hours_distribution:
            if name=="textbox":
                handled = self.handle_header_footer_start(attrs["bbox"])
                if not handled:
                    self.record_location(attrs["bbox"])
                self.in_textbox = True
            elif self.in_textbox and name=="text":
                self.in_text = True

    def characters(self, data):
        if self.in_text:
            self.append_buffer(data)

    def endElement(self, name):
        if name=="page":
            self.sort_lines()
        elif not self.in_figure and not self.in_hours_distribution:
            if name=="textbox":
                handled = self.handle_header_footer_end()
                if not handled:
                    text = self.pop_buffer()
                    if text=="Hours Distribution by Time Code":
                        self.record_hours_distribution()
                    else:
                        self.record_text(text)
                self.in_textbox = False
            elif name=="text":
                self.in_text = False

    def sort_lines(self):
        """Helper function to perform page-level cleaning on line-level data.
        Manipulates the page buffer and clears the line buffer.
        """
        sorted_lines = sorted(
            self.line_buffer,
            key=lambda x: (-float(x[1]), float(x[0])),
        )
        self.line_buffer = []
        self.page_buffer.append(sorted_lines)

    def record_location(self, data):
        """Helper function to append new location data to the line buffer."""
        self.line_buffer.append(data.split(",")[:2])

    def record_text(self, data):
        """Helper function to append new text data to the line buffer."""
        self.line_buffer[-1].append(data)

    def record_hours_distribution(self):
        """Helper function to fudge some numbers. Inflate the y-dimension of
        the final value in order to better catch some footer elements,
        especially (sub)total rows.
        """
        self.in_hours_distribution = True
        self.line_buffer[-1][1] = str(float(self.line_buffer[-1][1]) + 5)

    def append_buffer(self, data):
        """Helper function to append new character data to the text buffer."""
        self.text_buffer += data

    def pop_buffer(self):
        """Helper function to grab aggregated character data and reset the
        text buffer.
        """
        data = self.text_buffer
        self.text_buffer = ""
        return data.strip()

    def debug_assert(self, value, should_be, label=""):
        """Helper function to manage validation logic and conditional print
        statements.
        """
        if label:
            printf_label = f"{self.daterange}:{self.pagenum}:{label}:"
        else:
            printf_label = f"{self.daterange}:{self.pagenum}:"

        if isinstance(should_be, tuple):
            if value not in should_be:
                printf(
                    printf_label + "should be {0}, is {1}",
                    "one of " + ", ".join(should_be),
                    value,
                )
        elif value != should_be:
            printf(printf_label + "should be {0}, is {1}", should_be, value)

    def handle_header_footer_start(self, location):
        """Handle header and footer content on a page.

        There is some branching based on whether the current page is the first
        page of a document.

        If a header or footer box is identified (based on its location),
        the parser state is updated so that box's content can be grabbed,
        validated, and suppressed from output upon reaching its end.

        If a box is handled, return True. Otherwise return False to signal
        that further handling is necessary.
        """
        location_xy = location.split(",")[:2]
        identity = ','.join(location_xy)
        loc = (int(float(location_xy[0])), int(float(location_xy[1])), )

        if self.pagenum == "1":
            if is_approximately(loc, (335, 524, )):
                self.in_header_footer_parts["timesheet_label"] = True
                return True

            elif is_approximately(loc, (335, 504, )):
                self.in_header_footer_parts["timesheet_value"] = True
                return True

            elif is_approximately(loc, (20, 481, )):
                self.in_header_footer_parts["location_label"] = True
                return True

            elif is_approximately(loc, (93, 481, )):
                self.in_header_footer_parts["location_value"] = True
                return True

            elif is_approximately(loc, (20, 466, )):
                self.in_header_footer_parts["department_label"] = True
                return True

            elif is_approximately(loc, (93, 466, )):
                self.in_header_footer_parts["department_value"] = True
                return True

            elif is_approximately(loc, (20, 452, )):
                self.in_header_footer_parts["employee_type_label"] = True
                return True

            elif is_approximately(loc, (93, 452, )):
                self.in_header_footer_parts["employee_type_value"] = True
                return True

            elif is_approximately(loc, (20, 437, )):
                self.in_header_footer_parts["location_default_label"] = True
                return True

            elif is_approximately(loc, (93, 437, )):
                self.in_header_footer_parts["location_default_value"] = True
                return True

            elif is_approximately(loc, (230, 481, )):
                self.in_header_footer_parts["function_label"] = True
                return True

            elif is_approximately(loc, (304, 481, )):
                self.in_header_footer_parts["function_value"] = True
                return True

            elif is_approximately(loc, (230, 466, )):
                self.in_header_footer_parts["exempt_label"] = True
                return True

            elif is_approximately(loc, (304, 466, )):
                self.in_header_footer_parts["exempt_value"] = True
                return True

            elif is_approximately(loc, (230, 452, )):
                self.in_header_footer_parts["status_label"] = True
                return True

            elif is_approximately(loc, (304, 452, )):
                self.in_header_footer_parts["status_value"] = True
                return True

            elif is_approximately(loc, (230, 437, )):
                self.in_header_footer_parts["_doc_no_label"] = True
                return True

            elif is_approximately(loc, (304, 437, )):
                self.in_header_footer_parts["_doc_no_value"] = True
                return True

            elif is_approximately(loc, (440, 481, )):
                self.in_header_footer_parts["post_status_label"] = True
                return True

            elif is_approximately(loc, (513, 481, )):
                self.in_header_footer_parts["post_status_value"] = True
                return True

            elif is_approximately(loc, (440, 466, )):
                self.in_header_footer_parts["validation_label"] = True
                return True

            elif is_approximately(loc, (513, 466, )):
                self.in_header_footer_parts["validation_value"] = True
                return True

            elif is_approximately(loc, (440, 452, )):
                self.in_header_footer_parts["datetime_label"] = True
                return True

            elif is_approximately(loc, (513, 452, )):
                self.in_header_footer_parts["datetime_value"] = True
                return True

            elif is_approximately(loc, (651, 481, )):
                self.in_header_footer_parts["total_timesheet_label"] = True
                return True

            elif is_approximately(loc, (751, 481, )):
                self.in_header_footer_parts["total_timesheet_value"] = True
                return True

            elif is_approximately(loc, (651, 466, )):
                self.in_header_footer_parts["standard_hours_label"] = True
                return True

            elif is_approximately(loc, (751, 466, )):
                self.in_header_footer_parts["standard_hours_value"] = True
                return True

            elif is_approximately(loc, (651, 452, )):
                self.in_header_footer_parts["total_billable_label"] = True
                return True

            elif is_approximately(loc, (751, 452, )):
                self.in_header_footer_parts["total_billable_value"] = True
                return True

            elif is_approximately(loc, (651, 437, )):
                self.in_header_footer_parts["percent_billability_label"] = True
                return True

            elif is_approximately(loc, (751, 437, )):
                self.in_header_footer_parts["percent_billability_value"] = True
                return True

            elif is_approximately(loc, (20, 399, )):
                self.in_header_footer_parts["id_label"] = True
                return True

            elif is_approximately(loc, (40, 399, )):
                self.in_header_footer_parts["time_code_label"] = True
                return True

            elif is_approximately(loc, (120, 399, )):
                self.in_header_footer_parts["project_label"] = True
                return True

            elif is_approximately(loc, (200, 399, )):
                self.in_header_footer_parts["timetype_label"] = True
                return True

            elif loc[1]==399 and 370 <= loc[0] <= 730:
                self.in_header_footer_parts["day_label"] = True
                return True

            elif is_approximately(loc, (754, 399, )):
                self.in_header_footer_parts["total_label"] = True
                return True

            elif is_approximately(loc, (20, 27, )):
                self.in_header_footer_parts["footer_datetime_value"] = True
                return True

            elif is_approximately(loc, (120, 27, )):
                self.in_header_footer_parts["footer_timezone_value"] = True
                return True

            elif is_approximately(loc, (688, 27, )):
                self.in_header_footer_parts["footer_pagenum_value"] = True
                return True

            elif is_approximately(loc, (732, 27, )):
                self.in_header_footer_parts["footer_pagecount_value"] = True
                return True

        else:
            if is_approximately(loc, (333, 524, )):
                self.in_header_footer_parts["timesheet_label"] = True
                return True

            elif is_approximately(loc, (333, 504, )):
                self.in_header_footer_parts["timesheet_value"] = True
                return True

            elif is_approximately(loc, (20, 479, )):
                self.in_header_footer_parts["id_label"] = True
                return True

            elif is_approximately(loc, (40, 479, )):
                self.in_header_footer_parts["time_code_label"] = True
                return True

            elif is_approximately(loc, (120, 479, )):
                self.in_header_footer_parts["project_label"] = True
                return True

            elif is_approximately(loc, (200, 479, )):
                self.in_header_footer_parts["timetype_label"] = True
                return True

            elif loc[1]==479 and 370 <= loc[0] <= 730:
                self.in_header_footer_parts["day_label"] = True
                return True

            elif is_approximately(loc, (754, 479, )):
                self.in_header_footer_parts["total_label"] = True
                return True

            elif is_approximately(loc, (20, 27, )):
                self.in_header_footer_parts["footer_datetime_value"] = True
                return True

            elif is_approximately(loc, (120, 27, )):
                self.in_header_footer_parts["footer_timezone_value"] = True
                return True

            elif is_approximately(loc, (688, 27, )):
                self.in_header_footer_parts["footer_pagenum_value"] = True
                return True

            elif is_approximately(loc, (732, 27, )):
                self.in_header_footer_parts["footer_pagecount_value"] = True
                return True

        return False

    def handle_header_footer_end(self):
        """Handle header and footer content on a page.

        If a box is handled, return True. Otherwise return False to signal
        that further handling is necessary.
        """
        if self.in_header_footer_parts["_doc_no_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Doc.No.", label="doc no label")
            self.in_header_footer_parts["_doc_no_label"] = False
            return True

        elif self.in_header_footer_parts["_doc_no_value"]:
            value = self.pop_buffer()
            self.debug_assert(value, "1", label="doc no")
            self.in_header_footer_parts["_doc_no_value"] = False
            return True

        elif self.in_header_footer_parts["timesheet_label"]:
            value = self.pop_buffer()
            self.debug_assert(
                value,
                "Timesheet\n[109015] Ricottone, Dominic",
                label="timesheet label",
            )
            self.in_header_footer_parts["timesheet_label"] = False
            return True

        elif self.in_header_footer_parts["timesheet_value"]:
            self.daterange = self.pop_buffer()
            self.in_header_footer_parts["timesheet_value"] = False
            return True

        elif self.in_header_footer_parts["location_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Location:", label="location label")
            self.in_header_footer_parts["location_label"] = False
            return True

        elif self.in_header_footer_parts["location_value"]:
            value = self.pop_buffer()
            self.debug_assert(value, "[E01] Fors Marsh Group", label="location")
            self.in_header_footer_parts["location_value"] = False
            return True

        elif self.in_header_footer_parts["department_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Department:", label="department label")
            self.in_header_footer_parts["department_label"] = False
            return True

        elif self.in_header_footer_parts["department_value"]:
            value = self.pop_buffer()
            self.debug_assert(
                value,
                ("[3200] Advanced Analytics", "[3230] Data Management", ),
                label="department",
            )
            self.in_header_footer_parts["department_value"] = False
            return True

        elif self.in_header_footer_parts["employee_type_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Employee Type:", label="employee type label")
            self.in_header_footer_parts["employee_type_label"] = False
            return True

        elif self.in_header_footer_parts["employee_type_value"]:
            value = self.pop_buffer()
            self.debug_assert(value, "[1] Annual Salary", label="employee type")
            self.in_header_footer_parts["employee_type_value"] = False
            return True

        elif self.in_header_footer_parts["location_default_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Location (Default", label="location default label")
            self.in_header_footer_parts["location_default_label"] = False
            return True

        elif self.in_header_footer_parts["location_default_value"]:
            value = self.pop_buffer()
            self.debug_assert(value, "[LOCAL] Location", label="location default")
            self.in_header_footer_parts["location_default_value"] = False
            return True

        elif self.in_header_footer_parts["function_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Function:", label="function label")
            self.in_header_footer_parts["function_label"] = False
            return True

        elif self.in_header_footer_parts["function_value"]:
            value = self.pop_buffer()
            self.debug_assert(value, "[1] Full Time", label="function")
            self.in_header_footer_parts["function_value"] = False
            return True

        elif self.in_header_footer_parts["exempt_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Exempt:", label="exempt label")
            self.in_header_footer_parts["exempt_label"] = False
            return True

        elif self.in_header_footer_parts["exempt_value"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Yes", label="exempt")
            self.in_header_footer_parts["exempt_value"] = False
            return True

        elif self.in_header_footer_parts["status_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Status:", label="status label")
            self.in_header_footer_parts["status_label"] = False
            return True

        elif self.in_header_footer_parts["status_value"]:
            value = self.pop_buffer()
            self.debug_assert(
                value,
                ("Approved", "Closed", "On Hold [Draft]", ),
                label="status",
            )
            self.in_header_footer_parts["status_value"] = False
            return True

        elif self.in_header_footer_parts["post_status_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Post Status:", label="post status label")
            self.in_header_footer_parts["post_status_label"] = False
            return True

        elif self.in_header_footer_parts["post_status_value"]:
            value = self.pop_buffer()
            self.debug_assert(
                value,
                ("Posted", "Not posted", ),
                label="post status",
            )
            self.in_header_footer_parts["post_status_value"] = False
            return True

        elif self.in_header_footer_parts["validation_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Validation:", label="validation label")
            self.in_header_footer_parts["validation_label"] = False
            return True

        elif self.in_header_footer_parts["validation_value"]:
            value = self.pop_buffer()
            self.debug_assert(
                value,
                ("Passed", "Warnings", ),
                label="validation",
            )
            self.in_header_footer_parts["validation_value"] = False
            return True

        elif self.in_header_footer_parts["datetime_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Date/Time:", label="datetime label")
            self.in_header_footer_parts["datetime_label"] = False
            return True

        elif self.in_header_footer_parts["datetime_value"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["datetime_value"] = False
            return True

        elif self.in_header_footer_parts["total_timesheet_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Total Timesheet:", label="total timesheet label")
            self.in_header_footer_parts["total_timesheet_label"] = False
            return True

        elif self.in_header_footer_parts["total_timesheet_value"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["total_timesheet_value"] = False
            return True

        elif self.in_header_footer_parts["standard_hours_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Standard Hours:", label="standard hours label")
            self.in_header_footer_parts["standard_hours_label"] = False
            return True

        elif self.in_header_footer_parts["standard_hours_value"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["standard_hours_value"] = False
            return True

        elif self.in_header_footer_parts["total_billable_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Total Billable:", label="total billable label")
            self.in_header_footer_parts["total_billable_label"] = False
            return True

        elif self.in_header_footer_parts["total_billable_value"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["total_billable_value"] = False
            return True

        elif self.in_header_footer_parts["percent_billability_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Percent Billability:", label="percent billability label")
            self.in_header_footer_parts["percent_billability_label"] = False
            return True

        elif self.in_header_footer_parts["percent_billability_value"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["percent_billability_value"] = False
            return True

        elif self.in_header_footer_parts["id_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "ID", label="id label")
            self.in_header_footer_parts["id_label"] = False
            return True

        elif self.in_header_footer_parts["time_code_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Time Code", label="time code label")
            self.in_header_footer_parts["time_code_label"] = False
            return True

        elif self.in_header_footer_parts["project_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "Project", label="project label")
            self.in_header_footer_parts["project_label"] = False
            return True

        elif self.in_header_footer_parts["timetype_label"]:
            value = self.pop_buffer()
            self.debug_assert(value, "TimeType", label="timetype label")
            self.in_header_footer_parts["timetype_label"] = False
            return True

        elif self.in_header_footer_parts["day_label"]:
            value = self.pop_buffer()
            self.debug_assert(
                value,
                ("Mon", "Tue Wed", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", ),
                label="day label",
            )
            self.in_header_footer_parts["day_label"] = False
            return True

        elif self.in_header_footer_parts["total_label"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["total_label"] = False
            return True

        elif self.in_header_footer_parts["footer_datetime_value"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["footer_datetime_value"] = False
            return True

        elif self.in_header_footer_parts["footer_timezone_value"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["footer_timezone_value"] = False
            return True

        elif self.in_header_footer_parts["footer_pagenum_value"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["footer_pagenum_value"] = False
            return True

        elif self.in_header_footer_parts["footer_pagecount_value"]:
            value = self.pop_buffer()
            self.in_header_footer_parts["footer_pagecount_value"] = False
            return True

        return False

def parse(filename_in, filename_out):
    """Main routine. Reads an XML file and writes a CSV file."""
    parser = make_parser()
    handler = TimeSheetHandler()

    parser.setContentHandler(handler)
    parser.parse(filename_in)

    with open(filename_out, "w", newline="") as f:
        writer = csv.writer(f)
        for page in handler.page_buffer:
            for line in page:
                writer.writerow(line)