Python Forum
pyPDF2 nautilus columns modification
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyPDF2 nautilus columns modification
#1
Hi there, I am a recent convert to Linux and really enjoying it so far but I have come across a really annoying problem that is preventing me from moving over fully. I do a lot of work with pdf files and have over 15,000 of them in a folder to work with. I frequently have to do searches and to make things easier I have information stored in the “keywords” metadata section of the pdf. In windows I can view all the pf thumbnails using windows explorer and can also display the pdf tags using a COM program called pdf property extension.

https://coolsoft.altervista.org/en/pdfpropertyextension

I need something similar for linux and thought the nautilus columns python extension for nautilus would do the job for me.

https://www.linuxuprising.com/2019/02/na...adata.html

While it does exactly what I want, in its default form it only displays title and author, not keywords.
I managed to get into the pythons code that controls it and you can see the code here.

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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of nautilus-columns
 
# this script can installed to the current user account by running the
# following commands:
 
# sudo apt-get install python-FileManager python-mutagen python-pyexiv2
# python-kaa-metadata
 
# mkdir ~/.local/share/FileManager-python/extensions/
# cp bsc-v2.py ~/.local/share/FileManager-python/extensions/
# chmod a+x ~/.local/share/FileManager-python/extensions/bsc-v2.py
 
# alternatively, you can be able to place the script in:
# /usr/share/FileManager-python/extensions/
 
# change log:
# geb666: original bsc.py, based on work by Giacomo Bordiga
# jmdsdf: version 2 adds extra ID3 and EXIF tag support
# jmdsdf: added better error handling for ID3 tags, added mp3 length support,
#         distinguished between exif image size and true image size
# SabreWolfy: set consistent hh:mm:ss format, fixed bug with no ID3 information
#             throwing an unhandled exception
# jmdsdf: fixed closing file handles with mpinfo (thanks gueba)
# jmdsdf: fixed closing file handles when there's an exception
#         (thanks Pitxyoki)
# jmdsdf: added video parsing (work based on enbeto, thanks!)
# jmdsdf: added FLAC audio parsing through kaa.metadata
#         (thanks for the idea l-x-l)
# jmdsdf: added trackno, added mkv file support (thanks ENigma885)
# jmdsdf: added date/album for flac/video (thanks eldon.t)
# jmdsdf: added wav file support thru pyexiv2
# jmdsdf: added sample rate file support thru mutagen and kaa
#         (thanks for the idea N'ko)
# jmdsdf: fix with tracknumber for FLAC, thanks l-x-l
# draxus: support for pdf files
# arun (engineerarun@gmail.com): made changes to work with naulitus 3.x
# Andrew@webupd8.org: get EXIF support to work with FileManager 3
# Julien Blanc: fix bug caused by missing Exif.Image.Software key
# Andreas Schönfelder: show stars as rating
 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
import gi
try:
    gi.require_version('Nautilus', '3.0')
    gi.require_version('GObject', '2.0')
    gi.require_version('GExiv2', "0.10")
except Exception as e:
    print(e)
    exit(1)
from gi.repository import Nautilus as FileManager
from gi.repository import GObject
from gi.repository import GExiv2
 
import urllib
# for id3 support
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MPEGInfo
# for reading videos. for future improvement, this can also read mp3!
import kaa.metadata
# for reading image dimensions
from PIL import Image
# for reading pdf
from PyPDF2 import PdfFileReader
# locale
import sys
import os
import math
import locale
import gettext
 
APP = 'nautilus-columns'
ROOTDIR = '/usr/share/'
LANGDIR = os.path.join(ROOTDIR, 'locale-langpack')
 
try:
    current_locale, encoding = locale.getdefaultlocale()
    language = gettext.translation(APP, LANGDIR, [current_locale])
    language.install()
    if sys.version_info[0] == 3:
        _ = language.gettext
    else:
        _ = language.ugettext
except Exception as e:
    print(e)
    _ = str
 
 
def get_resolution_unit(metadata):
    try:
        value = metadata.get_tag_string('Exif.Image.ResolutionUnit')
    except Exception:
        value == -1
    if value == '1':
        return _('No absolute unit of measurement')
    elif value == '2':
        return _('Inch')
    elif value == '3':
        return _('Centimeter')
    return _('Unknown')
 
 
def get_orientation(metadata):
    try:
        value = metadata.get_orientation()
    except Exception:
        value == -1
    if value == GExiv2.Orientation.UNSPECIFIED:
        return _('Unknown')
    elif value == GExiv2.Orientation.NORMAL:
        return _('Normal')
    elif value == GExiv2.Orientation.HFLIP or\
            value == GExiv2.Orientation.VFLIP or\
            value == GExiv2.Orientation.ROT_90_HFLIP or\
            value == GExiv2.Orientation.ROT_90_VFLIP:
        return _('Flipped')
    elif value == GExiv2.Orientation.ROT_180:
        return _('180º')
    elif value == GExiv2.Orientation.ROT_90:
        return _('90º')
    elif value == GExiv2.Orientation.ROT_270:
        return _('270º')
    return _('Unknown')
 
 
def get_metering_mode(metadata):
    try:
        value = metadata.get_tag_string('Exif.Photo.MeteringMode')
    except Exception:
        value = -1
    if value == '0':
            return _('Unknown')
    elif value == '1':
            return _('Average')
    elif value == '2':
            return _('Center weighted average')
    elif value == '3':
            return _('Spot')
    elif value == '4':
            return _('Multi spot')
    elif value == '5':
            return _('Pattern')
    elif value == '6':
            return _('Partial')
    elif value == '255':
            return _('other ')
    return _('Unknown')
 
 
def get_light_source(metadata):
    try:
        value = metadata.get_tag_string('Exif.Photo.LightSource')
    except Exception:
        value = -1
    if value == '0':
            return _('Unknown')
    elif value == '1':
            return _('Daylight')
    elif value == '2':
            return _('Fluorescent')
    elif value == '3':
            return _('Tungsten (incandescent light)')
    elif value == '4':
            return _('Flash')
    elif value == '9':
            return _('Fine weather')
    elif value == '10':
            return _('Cloudy weather')
    elif value == '11':
            return _('Shade')
    elif value == '12':
            return _('Daylight fluorescent (D 5700 - 7100K)')
    elif value == '13':
            return _('Day white fluorescent (N 4600 - 5400K)')
    elif value == '14':
            return _('Cool white fluorescent (W 3900 - 4500K)')
    elif value == '15':
            return _('White fluorescent (WW 3200 - 3700K)')
    elif value == '17':
            return _('Standard light A')
    elif value == '18':
            return _('Standard light B')
    elif value == '19':
            return _('Standard light C')
    elif value == '20':
            return _('D55')
    elif value == '21':
            return _('D65')
    elif value == '22':
            return _('D75')
    elif value == '23':
            return _('D50')
    elif value == '24':
            return _('ISO studio tungsten')
    elif value == '255':
            return _('Other light source')
    return _('Unknown')
 
 
def get_exposure_mode(metadata):
    try:
        value = metadata.get_tag_string('Exif.Photo.ExposureMode')
    except Exception:
        value = -1
    if value == '0':
        return _('Auto exposure')
    elif value == '1':
        return _('Manual exposure')
    elif value == '2':
        return _('Auto bracket')
    return _('Unknown')
 
 
def get_gain_control(metadata):
    try:
        value = metadata.get_tag_string('Exif.Photo.GainControl')
    except Exception:
        value = -1
    if value == '0':
        return _('None')
    elif value == '1':
        return _('Low Gain Up')
    elif value == '2':
        return _('High Gain Up')
    elif value == '3':
        return _('Low Gain Down')
    elif value == '4':
        return _('High Gain Down')
    return _('Unknown')
 
 
def get_flash(metadata):
    try:
        value = metadata.get_tag_string('Exif.Photo.Flash')
    except Exception:
        value = -1
    if value == '0':
        return _('Flash did not fire')
    elif value == '1':
        return _('Flash fired')
    elif value == '2':
        return _('Strobe return light detected')
    elif value == '4':
        return _('Strobe return light not detected')
    elif value == '8':
        return _('Compulsory flash mode')
    elif value == '16':
        return _('Auto mode')
    elif value == '32':
        return _('No flash function')
    elif value == '64':
        return _('Red eye reduction mode')
    return _('Unknown')
 
 
class ColumnExtension(GObject.GObject,
                      FileManager.ColumnProvider,
                      FileManager.InfoProvider):
    def __init__(self):
        pass
 
    def get_columns(self):
        return (
            FileManager.Column(name='FileManagerPython::title_column',
                               attribute='title',
                               label=_('Title'),
                               description=_('Song title')),
            FileManager.Column(name='FileManagerPython::album_column',
                               attribute='album',
                               label=_('Album'),
                               description=_('Album')),
            FileManager.Column(name='FileManagerPython::artist_column',
                               attribute='artist',
                               label=_('Artist'),
                               description=_('Artist')),
            FileManager.Column(name='FileManagerPython::tracknumber_column',
                               attribute='tracknumber',
                               label=_('Track'),
                               description=_('Track number')),
            FileManager.Column(name='FileManagerPython::genre_column',
                               attribute='genre',
                               label=_('Genre'),
                               description=_('Genre')),
            FileManager.Column(name='FileManagerPython::date_column',
                               attribute='date',
                               label=_('Date'),
                               description=_('Date')),
            FileManager.Column(name='FileManagerPython::bitrate_column',
                               attribute='bitrate',
                               label=_('Bitrate'),
                               description=_('Audio Bitrate in kilo bits per \
second')),
            FileManager.Column(name='FileManagerPython::samplerate_column',
                               attribute='samplerate',
                               label=_('Sample rate'),
                               description=_('Sample rate in Hz')),
            FileManager.Column(name='FileManagerPython::length_column',
                               attribute='length',
                               label=_('Length'),
                               description=_('Length of audio')),
            # Images
            FileManager.Column(name='FileManagerPython::exposure_time_column',
                               attribute='exposure_time',
                               label=_('Exposure time'),
                               description=_('Exposure time in seconds')),
            FileManager.Column(name='FileManagerPython::fnumber_column',
                               attribute='fnumber',
                               label=_('F number'),
                               description=_('Exposure F number')),
            FileManager.Column(name='FileManagerPython::fnumber_focal_length',
                               attribute='focal_length',
                               label=_('Focal length'),
                               description=_('The actual focal length of the \
lens, in mm.')),
            FileManager.Column(name='FileManagerPython::gps_altitude_column',
                               attribute='gps_altitude',
                               label=_('Altitude'),
                               description=_('GPS Altitude')),
            FileManager.Column(name='FileManagerPython::gps_latitude_column',
                               attribute='gps_latitude',
                               label=_('Latitude'),
                               description=_('GPS Latitude')),
            FileManager.Column(name='FileManagerPython::gps_longitude_column',
                               attribute='gps_longitude',
                               label=_('Longitude'),
                               description=_('GPS Longitude')),
            FileManager.Column(name='FileManagerPython::iso_speed_column',
                               attribute='iso_speed',
                               label=_('ISO'),
                               description=_('ISO Speed')),
            FileManager.Column(name='FileManagerPython::orientation_column',
                               attribute='orientation',
                               label=_('Orientation'),
                               description=_('Orientation')),
            FileManager.Column(name='FileManagerPython::model_column',
                               attribute='model',
                               label=_('Model'),
                               description=_('Model')),
            FileManager.Column(name='\
FileManagerPython::resolution_unit_column',
                               attribute='resolution_unit',
                               label=_('Resolution unit'),
                               description=_('The unit for measuring')),
            FileManager.Column(name='FileManagerPython::xresolution_column',
                               attribute='xresolution',
                               label=_('X resolution'),
                               description=_('The resolution in the x axis')),
            FileManager.Column(name='FileManagerPython::yresolution_column',
                               attribute='yresolution',
                               label=_('Y resolution'),
                               description=_('The resolution in the y axis')),
            FileManager.Column(name='\
FileManagerPython::datetime_original_column',
                               attribute='datetime_original',
                               label=_('Capture date'),
                               description=_('Photo capture date')),
            FileManager.Column(name='\
FileManagerPython::shutter_speed_value_column',
                               attribute='shutter_speed_value',
                               label=_('Shutter speed'),
                               description=_('Shutter speed')),
            FileManager.Column(name='\
FileManagerPython::aperture_value_column',
                               attribute='aperture_value',
                               label=_('Aperture'),
                               description=_('The lens aperture')),
            FileManager.Column(name='\
FileManagerPython::brightness_value_column',
                               attribute='brightness_value',
                               label=_('Brightness'),
                               description=_('Brightness')),
            FileManager.Column(name='\
FileManagerPython::exposure_bias_value_column',
                               attribute='exposure_bias_value',
                               label=_('Exposure'),
                               description=_('The exposure bias')),
            FileManager.Column(name='\
FileManagerPython::max_aperture_value_column',
                               attribute='max_aperture_value',
                               label=_('Max aperture'),
                               description=_(
                                    'The smallest F number of the lens')),
            FileManager.Column(name='\
FileManagerPython::metering_mode_column',
                               attribute='metering_mode',
                               label=_('Metering mode'),
                               description=_('The metering mode')),
            FileManager.Column(name='\
FileManagerPython::light_source_column',
                               attribute='light_source',
                               label=_('Light source'),
                               description=_('The kind of light source')),
            FileManager.Column(name='\
FileManagerPython::flash_column',
                               attribute='flash',
                               label=_('Flash'),
                               description=_('Indicates the status of flash \
when the image was shot')),
            FileManager.Column(name='\
FileManagerPython::exposure_mode_column',
                               attribute='exposure_mode',
                               label=_('Exposure mode'),
                               description=_('The exposure mode set when the \
image was shot')),
            FileManager.Column(name='\
FileManagerPython::gain_control_column',
                               attribute='gain_control',
                               label=_('Gain control'),
                               description=_('The degree of overall image \
gain adjustment')),
            FileManager.Column(name='\
FileManagerPython::width_column',
                               attribute='width',
                               label=_('Width'),
                               description=_(
                                    'Image/video/pdf width (pixel/mm)')),
            FileManager.Column(name='\
FileManagerPython::height_column',
                               attribute='height',
                               label=_('Height'),
                               description=_(
                                    'Image/video/pdf height (pixel/mm)')),
            FileManager.Column(name='\
FileManagerPython::pages_column',
                               attribute='pages',
                               label=_('Pages'),
                               description=_('Number of pages')),
        )
 
    def update_file_info(self, file):
        # set defaults to blank
        file.add_string_attribute('title', '')
        file.add_string_attribute('album', '')
        file.add_string_attribute('artist', '')
        file.add_string_attribute('tracknumber', '')
        file.add_string_attribute('genre', '')
        file.add_string_attribute('date', '')
        file.add_string_attribute('bitrate', '')
        file.add_string_attribute('samplerate', '')
        file.add_string_attribute('length', '')
        file.add_string_attribute('datetime_original', '')
        file.add_string_attribute('exposure_time', '')
        file.add_string_attribute('fnumber', '')
        file.add_string_attribute('focal_length', '')
        file.add_string_attribute('gps_altitude', '')
        file.add_string_attribute('gps_latitude', '')
        file.add_string_attribute('gps_longitude', '')
        file.add_string_attribute('iso_speed', '')
        file.add_string_attribute('get_orientation', '')
        file.add_string_attribute('model', '')
        file.add_string_attribute('resolution_unit', '')
        file.add_string_attribute('xresolution', '')
        file.add_string_attribute('yresolution', '')
        file.add_string_attribute('shutter_speed_value', '')
        file.add_string_attribute('aperture_value', '')
        file.add_string_attribute('brightness_value', '')
        file.add_string_attribute('exposure_bias_value', '')
        file.add_string_attribute('max_aperture_value', '')
        file.add_string_attribute('metering_mode', '')
        file.add_string_attribute('light_source', '')
        file.add_string_attribute('flash', '')
        file.add_string_attribute('exposure_mode', '')
        file.add_string_attribute('gain_control', '')
        file.add_string_attribute('width', '')
        file.add_string_attribute('height', '')
        file.add_string_attribute('pages', '')
 
        if file.get_uri_scheme() != 'file':
            return
 
        # strip file:// to get absolute path
        filename = urllib.unquote(file.get_uri()[7:])
 
        # mp3 handling
        if file.is_mime_type('audio/mpeg'):
            # attempt to read ID3 tag
            try:
                audio = EasyID3(filename)
                # sometimes the audio variable will not have one of these items
                # defined, that's why there is this long try / except attempt
                try:
                    if 'title' in audio.keys():
                        file.add_string_attribute('title', audio['title'][0])
                    else:
                        file.add_string_attribute('title', '')
                except Exception:
                    file.add_string_attribute('title', _('Error'))
                try:
                    file.add_string_attribute('album', audio['album'][0])
                except Exception:
                    file.add_string_attribute('album', _('Error'))
                try:
                    file.add_string_attribute('artist', audio['artist'][0])
                except Exception:
                    file.add_string_attribute('artist', _('Error'))
                try:
                    file.add_string_attribute('tracknumber',
                                              audio['tracknumber'][0])
                except Exception:
                    file.add_string_attribute('tracknumber', _('Error'))
                try:
                    file.add_string_attribute('genre', audio['genre'][0])
                except Exception:
                    file.add_string_attribute('genre', _('Error'))
                try:
                    file.add_string_attribute('date', audio['date'][0])
                except Exception:
                    file.add_string_attribute('date', _('Error'))
            except Exception:
                # [SabreWolfy] some files have no ID3 tag and will throw this
                # exception:
                file.add_string_attribute('title', '')
                file.add_string_attribute('album', '')
                file.add_string_attribute('artist', '')
                file.add_string_attribute('tracknumber', '')
                file.add_string_attribute('genre', '')
                file.add_string_attribute('date', '')
            # try to read MP3 information (bitrate, length, samplerate)
            try:
                mpfile = open(filename)
                mpinfo = MPEGInfo(mpfile)
                file.add_string_attribute(
                    'bitrate', str(mpinfo.bitrate / 1000) + ' Kbps')
                file.add_string_attribute(
                    'samplerate', str(mpinfo.sample_rate) + ' Hz')
                # [SabreWolfy] added consistent formatting of times in format
                # hh:mm:ss
                # [SabreWolfy[ to allow for correct column sorting by length
                mp3length = '%02i:%02i:%02i' % ((int(mpinfo.length / 3600)),
                                                (int(mpinfo.length / 60 % 60)),
                                                (int(mpinfo.length % 60)))
                mpfile.close()
                file.add_string_attribute('length', mp3length)
            except Exception:
                file.add_string_attribute('bitrate', _('Error'))
                file.add_string_attribute('length', _('Error'))
                file.add_string_attribute('samplerate', _('Error'))
                try:
                    mpfile.close()
                except Exception:
                    pass
 
        # image handling
        if file.get_mime_type().split('/')[0] in ('image'):
            # EXIF handling routines
            try:
                metadata = GExiv2.Metadata(filename)
                try:
                    file.add_string_attribute(
                        'datetime_original',
                        metadata.get_tag_string('Exif.Image.DateTime'))
                except Exception:
                    file.add_string_attribute('datetime_original', '')
                try:
                    file.add_string_attribute(
                        'artist',
                        metadata.get_tag_string('Exif.Image.Artist'))
                except Exception:
                    file.add_string_attribute('artist', '')
                try:
                    file.add_string_attribute(
                        'title',
                        metadata.get_tag_string('Exif.Image.ImageDescription'))
                except Exception:
                    file.add_string_attribute('title', '')
                try:
                    file.add_string_attribute(
                        'exposure_time',
                        metadata.get_exposure_time())
                except Exception:
                    file.add_string_attribute('exposure_time', '')
                try:
                    file.add_string_attribute(
                        'fnumber',
                        metadata.get_fnumber())
                except Exception:
                    file.add_string_attribute('fnumber', '')
                try:
                    file.add_string_attribute(
                        'focal_length',
                        metadata.get_focal_length())
                except Exception:
                    file.add_string_attribute('focal_length', '')
                try:
                    file.add_string_attribute(
                        'gps_altitude',
                        metadata.get_gps_altitude())
                except Exception:
                    file.add_string_attribute('gps_altitude', '')
                try:
                    file.add_string_attribute(
                        'gps_latitude',
                        metadata.get_gps_latitude())
                except Exception:
                    file.add_string_attribute('gps_latitude', '')
                try:
                    file.add_string_attribute(
                        'gps_longitude',
                        metadata.get_gps_longitude())
                except Exception:
                    file.add_string_attribute('gps_longitude', '')
                try:
                    file.add_string_attribute(
                        'iso_speed',
                        metadata.get_iso_speed())
                except Exception:
                    file.add_string_attribute('iso_speed', '')
                file.add_string_attribute('orientation',
                                          get_orientation(metadata))
                try:
                    file.add_string_attribute(
                        'model',
                        metadata.get_tag_string('Exif.Image.Model'))
                except Exception:
                    file.add_string_attribute('model', '')
                file.add_string_attribute('resolution_unit',
                                          get_resolution_unit(metadata))
                try:
                    file.add_string_attribute(
                        'xresolution',
                        metadata.get_tag_string('Exif.Image.XResolution'))
                except Exception:
                    file.add_string_attribute('xresolution', '')
                try:
                    file.add_string_attribute(
                        'yresolution',
                        metadata.get_tag_string('Exif.Image.YResolution'))
                except Exception:
                    file.add_string_attribute('yresolution', '')
                try:
                    file.add_string_attribute(
                        'shutter_speed_value',
                        metadata.get_tag_string(
                            'Exif.Photo.ShutterSpeedValue'))
                except Exception:
                    file.add_string_attribute('shutter_speed_value', '')
                try:
                    file.add_string_attribute(
                        'aperture_value',
                        metadata.get_tag_string(
                            'Exif.Photo.ApertureValue'))
                except Exception:
                    file.add_string_attribute('aperture_value', '')
                try:
                    file.add_string_attribute(
                        'brightness_value',
                        metadata.get_tag_string(
                            'Exif.Photo.BrightnessValue'))
                except Exception:
                    file.add_string_attribute('brightness_value', '')
                try:
                    file.add_string_attribute(
                        'brightness_value',
                        metadata.get_tag_string(
                            'Exif.Photo.BrightnessValue'))
                except Exception:
                    file.add_string_attribute('brightness_value', '')
                try:
                    file.add_string_attribute(
                        'exposure_bias_value',
                        metadata.get_tag_string(
                            'Exif.Photo.ExposureBiasValue'))
                except Exception:
                    file.add_string_attribute('exposure_bias_value', '')
                try:
                    file.add_string_attribute(
                        'max_aperture_value',
                        metadata.get_tag_string(
                            'Exif.Photo.MaxApertureValue'))
                except Exception:
                    file.add_string_attribute('max_aperture_value', '')
                file.add_string_attribute(
                    'metering_mode',
                    get_metering_mode(metadata))
                file.add_string_attribute(
                    'light_source',
                    get_light_source(metadata))
                file.add_string_attribute(
                    'flash',
                    get_flash(metadata))
                file.add_string_attribute(
                    'exposure_mode',
                    get_exposure_mode(metadata))
                file.add_string_attribute(
                    'gain_control',
                    get_gain_control(metadata))
            except Exception:
                file.add_string_attribute('datetime_original', '')
                file.add_string_attribute('artist', '')
                file.add_string_attribute('title', '')
                file.add_string_attribute('exposure_time', '')
                file.add_string_attribute('fnumber', '')
                file.add_string_attribute('focal_length', '')
                file.add_string_attribute('gps_altitude', '')
                file.add_string_attribute('gps_latitude', '')
                file.add_string_attribute('gps_longitude', '')
                file.add_string_attribute('iso_speed', '')
                file.add_string_attribute('get_orientation', '')
                file.add_string_attribute('model', '')
                file.add_string_attribute('resolution_unit', '')
                file.add_string_attribute('xresolution', '')
                file.add_string_attribute('yresolution', '')
                file.add_string_attribute('shutter_speed_value', '')
                file.add_string_attribute('aperture_value', '')
                file.add_string_attribute('brightness_value', '')
                file.add_string_attribute('exposure_bias_value', '')
                file.add_string_attribute('max_aperture_value', '')
                file.add_string_attribute('metering_mode', '')
                file.add_string_attribute('light_source', '')
                file.add_string_attribute('flash', '')
                file.add_string_attribute('exposure_mode', '')
                file.add_string_attribute('gain_control', '')
            try:
                im = Image.open(filename)
                try:
                    file.add_string_attribute('width', str(im.size[0]))
                except Exception:
                    file.add_string_attribute('width', _('Error'))
                try:
                    file.add_string_attribute('height', str(im.size[1]))
                except Exception:
                    file.add_string_attribute('height', _('Error'))
            except Exception:
                file.add_string_attribute('width', '')
                file.add_string_attribute('height', '')
 
        # video/flac handling
        if file.is_mime_type('video/x-msvideo') or\
                file.is_mime_type('video/mpeg') or\
                file.is_mime_type('video/x-ms-wmv') or\
                file.is_mime_type('video/mp4') or\
                file.is_mime_type('audio/x-flac') or\
                file.is_mime_type('video/x-flv') or\
                file.is_mime_type('video/x-matroska') or\
                file.is_mime_type('audio/x-wav'):
            try:
                info = kaa.metadata.parse(filename)
                try:
                    file.add_string_attribute(
                        'length',
                        '%02i:%02i:%02i' % ((int(info.length / 3600)),
                                            (int(info.length / 60 % 60)),
                                            (int(info.length % 60))))
                except Exception:
                    file.add_string_attribute('length', _('Error'))
                try:
                    file.add_string_attribute('width',
                                              str(info.video[0].width))
                except Exception:
                    file.add_string_attribute('height', _('Error'))
                try:
                    file.add_string_attribute('height',
                                              str(info.video[0].height))
                except Exception:
                    file.add_string_attribute('width', _('Error'))
                try:
                    file.add_string_attribute(
                        'bitrate', str(round(info.audio[0].bitrate / 1000)))
                except Exception:
                    file.add_string_attribute('bitrate', _('Error'))
                try:
                    file.add_string_attribute(
                        'samplerate',
                        str(int(info.audio[0].samplerate)) + _(' Hz'))
                except Exception:
                    file.add_string_attribute('samplerate', _('Error'))
                try:
                    file.add_string_attribute('title', info.title)
                except Exception:
                    file.add_string_attribute('title', _('Error'))
                try:
                    file.add_string_attribute('artist', info.artist)
                except Exception:
                    file.add_string_attribute('artist', _('Error'))
                try:
                    file.add_string_attribute('genre', info.genre)
                except Exception:
                    file.add_string_attribute('genre', _('Error'))
                try:
                    file.add_string_attribute('tracknumber', info.trackno)
                except Exception:
                    file.add_string_attribute('tracknumber', _('Error'))
                try:
                    file.add_string_attribute('date', info.userdate)
                except Exception:
                    file.add_string_attribute('date', _('Error'))
                try:
                    file.add_string_attribute('album', info.album)
                except Exception:
                    file.add_string_attribute('album', _('Error'))
            except Exception:
                file.add_string_attribute('length', _('Error'))
                file.add_string_attribute('width', _('Error'))
                file.add_string_attribute('height', _('Error'))
                file.add_string_attribute('bitrate', _('Error'))
                file.add_string_attribute('samplerate', _('Error'))
                file.add_string_attribute('title', _('Error'))
                file.add_string_attribute('artist', _('Error'))
                file.add_string_attribute('genre', _('Error'))
                file.add_string_attribute('track', _('Error'))
                file.add_string_attribute('date', _('Error'))
                file.add_string_attribute('album', _('Error'))
        # pdf handling
        if file.is_mime_type('application/pdf'):
            try:
                f = open(filename, 'rb')
                pdf = PdfFileReader(f)
                info = pdf.getDocumentInfo()
                try:
                    file.add_string_attribute(
                        'title',
                        info.title if info.title is not None else '')
                except Exception:
                    file.add_string_attribute('title', _('Error'))
                try:
                    file.add_string_attribute(
                        'artist',
                        info.author if info.author is not None else '')
                except Exception:
                    file.add_string_attribute('artist', _('Error'))
                try:
                    file.add_string_attribute(
                        'pages',
                        str(pdf.getNumPages()))
                except Exception:
                    file.add_string_attribute('pages', _('Error'))
                if pdf.getNumPages() > 0:
                    try:
                        width = abs(pdf.getPage(0).mediaBox.upperRight[0] -
                                    pdf.getPage(0).mediaBox.lowerLeft[0])
                        file.add_string_attribute(
                            'width',
                            str(int(float(width) * math.sqrt(2.0) / 4.0)))
                    except Exception:
                        file.add_string_attribute('width', '')
                    try:
                        height = abs(pdf.getPage(0).mediaBox.upperRight[1] -
                                     pdf.getPage(0).mediaBox.lowerLeft[1])
                        file.add_string_attribute(
                            'height',
                            str(int(float(height) * math.sqrt(2.0) / 4.0)))
                    except Exception:
                        file.add_string_attribute('height', '')
                else:
                    file.add_string_attribute('width', '')
                    file.add_string_attribute('height', '')
                f.close()
            except Exception:
                file.add_string_attribute('title', _('Error'))
                file.add_string_attribute('artist', _('Error'))
                file.add_string_attribute('pages', _('Error'))
                file.add_string_attribute('width', _('Error'))
                file.add_string_attribute('height', _('Error'))
        self.get_columns()
the issue seems to be that the script uses the code getDocumentInfo() to pull the metadata from the pdf, but the keywords are stored in the xmpmetadata under the pdf_keywords and is accessed by the code getXmpMetadata () I have no idea how this could be incorporated into the script to make it work. I have managed to alter the first part of the script so I have the Keywords column appear in nautilus but it just shows an error so it is not extracting the information from the pdf properly.

Any help would be much appreciated.
Reply
#2
What's the error that you get?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Filer and sort files by modification time in a directory tester_V 5 2,556 May-02-2024, 05:39 PM
Last Post: tester_V
  PyPDF2 deprecation problem gowb0w 5 10,890 Sep-21-2023, 12:38 PM
Last Post: Pedroski55
  ModuleNotFoundError: No module named 'PyPDF2' Benitta2525 1 3,980 Aug-07-2023, 05:32 AM
Last Post: DPaul
  Pypdf2 will not find text standenman 2 1,864 Feb-03-2023, 10:52 PM
Last Post: standenman
  pyPDF2 PDFMerger close pensding file japo85 2 4,086 Jul-28-2022, 09:49 AM
Last Post: japo85
  PyPDF2 processing problem Pavel_47 6 12,838 May-04-2021, 06:58 AM
Last Post: chaitanya
  Problem with installing PyPDF2 Pavel_47 2 7,300 Nov-10-2019, 02:58 PM
Last Post: Pavel_47
  Scaled scatter modification yvrob 1 2,564 Nov-08-2019, 04:05 AM
Last Post: yvrob
  Python script modification rajannn 1 2,561 Oct-06-2019, 07:55 PM
Last Post: micseydel
  List modification returns none Mark17 6 4,228 Sep-02-2019, 05:40 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020