-
Notifications
You must be signed in to change notification settings - Fork 1
/
unoconv
1452 lines (1271 loc) · 69.6 KB
/
unoconv
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# 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: version 2 only.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright 2007-2010 Dag Wieers <[email protected]>
from __future__ import print_function
from distutils.version import LooseVersion
import getopt
import glob
import os
import signal
import subprocess
import sys
import time
__version__ = '0.8.2'
doctypes = ('document', 'graphics', 'presentation', 'spreadsheet')
global convertor, office, ooproc, product
ooproc = None
uno = unohelper = None
exitcode = 0
class Office:
def __init__(self, basepath, urepath, unopath, pyuno, binary, python, pythonhome):
self.basepath = basepath
self.urepath = urepath
self.unopath = unopath
self.pyuno = pyuno
self.binary = binary
self.python = python
self.pythonhome = pythonhome
def __str__(self):
return self.basepath
def __repr__(self):
return self.basepath
# Implement a path normalizer to make unoconv work on MacOS X, on
# which 'program' is a symlink to 'MacOSX,' which seems to break unoconv.
def realpath(*args):
"""Implement a combination of os.path.join(), os.path.abspath() and
os.path.realpath() in order to normalize path constructions."""
ret = ''
for arg in args:
ret = os.path.join(ret, arg)
return os.path.realpath(os.path.abspath(ret))
# The first thing we should do is find a suitable Office installation
# with a compatible pyuno library that we can import.
#
# See: http://user.services.openoffice.org/en/forum/viewtopic.php?f=45&t=36370&p=166783
def find_offices():
ret = []
extrapaths = []
# Try using UNO_PATH first (in many incarnations, we'll see what sticks).
if 'UNO_PATH' in os.environ:
extrapaths += [os.environ['UNO_PATH'],
os.path.dirname(os.environ['UNO_PATH']),
os.path.dirname(os.path.dirname(os.environ['UNO_PATH']))]
else:
if os.name in ('nt', 'os2'):
if 'PROGRAMFILES' in list(os.environ.keys()):
extrapaths += glob.glob(os.environ['PROGRAMFILES']+'\\LibreOffice*') + \
glob.glob(os.environ['PROGRAMFILES']+'\\OpenOffice.org*')
if 'PROGRAMFILES(X86)' in list(os.environ.keys()):
extrapaths += glob.glob(os.environ['PROGRAMFILES(X86)']+'\\LibreOffice*') + \
glob.glob(os.environ['PROGRAMFILES(X86)']+'\\OpenOffice.org*')
if 'PROGRAMW6432' in list(os.environ.keys()):
extrapaths += glob.glob(os.environ['PROGRAMW6432']+'\\LibreOffice*') + \
glob.glob(os.environ['PROGRAMW6432']+'\\OpenOffice.org*')
elif os.name == 'mac' or sys.platform == 'darwin':
extrapaths += ['/Applications/LibreOffice.app/Contents',
'/Applications/NeoOffice.app/Contents',
'/Applications/OpenOffice.app/Contents',
'/Applications/OpenOffice.org.app/Contents']
else:
extrapaths += glob.glob('/usr/lib*/libreoffice*') + \
glob.glob('/usr/lib*/openoffice*') + \
glob.glob('/usr/lib*/ooo*') + \
glob.glob('/opt/libreoffice*') + \
glob.glob('/opt/openoffice*') + \
glob.glob('/opt/ooo*') + \
glob.glob('/usr/local/libreoffice*') + \
glob.glob('/usr/local/openoffice*') + \
glob.glob('/usr/local/ooo*') + \
glob.glob('/usr/local/lib/libreoffice*')
# Find a working set for python UNO bindings.
for basepath in extrapaths:
if os.name in ('nt', 'os2'):
officelibraries = ('pyuno.pyd',)
officebinaries = ('soffice.exe',)
pythonbinaries = ('python.exe',)
pythonhomes = ()
elif os.name == 'mac' or sys.platform == 'darwin':
officelibraries = ('pyuno.so', 'libpyuno.dylib')
officebinaries = ('soffice.bin', 'soffice')
pythonbinaries = ('python.bin', 'python')
pythonhomes = ('OOoPython.framework/Versions/*/lib/python*')
else:
officelibraries = ('pyuno.so',)
officebinaries = ('soffice.bin',)
pythonbinaries = ('python.bin', 'python')
pythonhomes = ('python-core-*',)
# Older LibreOffice/OpenOffice and Windows use basis-link/ or basis/
libpath = 'error'
for basis in ('basis-link', 'basis', ''):
for lib in officelibraries:
for libdir in ('program', 'Frameworks'):
if os.path.isfile(realpath(basepath, basis, libdir, lib)):
libpath = realpath(basepath, basis, libdir)
officelibrary = realpath(libpath, lib)
info(3, "Found %s in %s" % (lib, libpath))
# Break the inner loop...
break
# Continue if the inner loop wasn't broken.
else:
continue
break
# Continue if the inner loop wasn't broken.
else:
continue
# Inner loop was broken, break the outer.
break
else:
continue
# MacOSX has soffice binaries installed in MacOS subdirectory, not program.
unopath = 'error'
for basis in ('basis-link', 'basis', ''):
for bin in officebinaries:
for bindir in ('program', '', 'MacOS'):
if os.path.isfile(realpath(basepath, basis, bindir, bin)):
unopath = realpath(basepath, basis, bindir)
officebinary = realpath(unopath, bin)
info(3, "Found %s in %s" % (bin, unopath))
# Break the inner loop.
break
# Continue if the inner loop wasn't broken.
else:
continue
break
# Continue if the inner loop wasn't broken.
else:
continue
# Inner loop was broken; break the outer.
break
else:
continue
# Windows does not provide or need a URE/lib directory?
urepath = ''
for basis in ('basis-link', 'basis', ''):
for ure in ('ure-link', 'ure', 'URE', ''):
if os.path.isfile(realpath(basepath, basis, ure, 'lib', 'unorc')):
urepath = realpath(basepath, basis, ure)
info(3, "Found %s in %s" % ('unorc', realpath(urepath, 'lib')))
# Break the inner loop.
break
# Continue if the inner loop wasn't broken.
else:
continue
# Inner loop was broken; break the outer.
break
pythonhome = None
for home in pythonhomes:
if glob.glob(realpath(libpath, home)):
pythonhome = glob.glob(realpath(libpath, home))[0]
info(3, "Found %s in %s" % (home, pythonhome))
break
# if not os.path.isfile(realpath(basepath, program, officebinary)):
# continue
# info(3, "Found %s in %s" % (officebinary, realpath(basepath, program)))
# if not glob.glob(realpath(basepath, basis, program, 'python-core-*')):
# continue
# Find suitable Python executable: regular unopath or MacOS version.
# LibreOffice 5.4.6.2 on MacOS X 10.13.3 ships the Python executable
# in the "Resources" folder.
pythonpath_candidates = [unopath, realpath(basepath, 'Resources')]
python_effective = find_executable(pythonpath_candidates, pythonbinaries)
if python_effective:
info(3, "Found Python at %s" % python_effective)
office = Office(basepath, urepath, unopath, officelibrary, officebinary,
python_effective, pythonhome)
else:
info(3, "Considering %s" % basepath)
office = Office(basepath, urepath, unopath, officelibrary, officebinary,
sys.executable, None)
ret.append(office)
return ret
def find_executable(folders, filenames):
for folder in folders:
for filename in filenames:
candidate = realpath(folder, filename)
if os.path.isfile(candidate):
return candidate
def office_environ(office):
# Set PATH so crash_report is found.
path_prefix = realpath(office.basepath, 'program') + os.pathsep + realpath(office.basepath, 'Resources')
if 'PATH' in os.environ:
os.environ['PATH'] = path_prefix + os.pathsep + os.environ['PATH']
else:
os.environ['PATH'] = path_prefix
# Set UNO_PATH so "officehelper.bootstrap()" can find soffice executable:
os.environ['UNO_PATH'] = office.unopath
# Set URE_BOOTSTRAP so "uno.getComponentContext()" bootstraps a complete UNO environment.
if os.name in ('nt', 'os2'):
os.environ['URE_BOOTSTRAP'] = 'vnd.sun.star.pathname:' + realpath(office.basepath, 'program', 'fundamental.ini')
else:
if os.path.isfile(realpath(office.basepath, 'program', 'fundamentalrc')):
os.environ['URE_BOOTSTRAP'] = 'vnd.sun.star.pathname:' + realpath(office.basepath, 'program', 'fundamentalrc')
else:
os.environ['URE_BOOTSTRAP'] = 'vnd.sun.star.pathname:' + realpath(office.basepath, 'Resources', 'fundamentalrc')
# Set LD_LIBRARY_PATH so that "import pyuno" finds libpyuno.so:
if 'LD_LIBRARY_PATH' in os.environ:
os.environ['LD_LIBRARY_PATH'] = office.unopath + os.pathsep + \
realpath(office.urepath, 'lib') + os.pathsep + \
os.environ['LD_LIBRARY_PATH']
else:
os.environ['LD_LIBRARY_PATH'] = office.unopath + os.pathsep + \
realpath(office.urepath, 'lib')
if office.pythonhome:
for libpath in (realpath(office.pythonhome, 'lib'),
realpath(office.pythonhome, 'lib', 'lib-dynload'),
realpath(office.pythonhome, 'lib', 'lib-tk'),
realpath(office.pythonhome, 'lib', 'site-packages'),
office.unopath):
sys.path.insert(0, libpath)
else:
# Still needed for system python using LibreOffice UNO bindings
# Although we prefer to use a system UNO binding in this case
sys.path.append(office.unopath)
def debug_office():
if 'URE_BOOTSTRAP' in os.environ:
print('URE_BOOTSTRAP=%s' % os.environ['URE_BOOTSTRAP'], file=sys.stderr)
if 'UNO_PATH' in os.environ:
print('UNO_PATH=%s' % os.environ['UNO_PATH'], file=sys.stderr)
if 'UNO_TYPES' in os.environ:
print('UNO_TYPES=%s' % os.environ['UNO_TYPES'], file=sys.stderr)
print('PATH=%s' % os.environ['PATH'])
if 'PYTHONHOME' in os.environ:
print('PYTHONHOME=%s' % os.environ['PYTHONHOME'], file=sys.stderr)
if 'PYTHONPATH' in os.environ:
print('PYTHONPATH=%s' % os.environ['PYTHONPATH'], file=sys.stderr)
if 'LD_LIBRARY_PATH' in os.environ:
print('LD_LIBRARY_PATH=%s' % os.environ['LD_LIBRARY_PATH'], file=sys.stderr)
def python_switch(office):
if office.pythonhome:
os.environ['PYTHONHOME'] = office.pythonhome
os.environ['PYTHONPATH'] = realpath(office.pythonhome, 'lib') + os.pathsep + \
realpath(office.pythonhome, 'lib', 'lib-dynload') + os.pathsep + \
realpath(office.pythonhome, 'lib', 'lib-tk') + os.pathsep + \
realpath(office.pythonhome, 'lib', 'site-packages') + os.pathsep + \
office.unopath
os.environ['UNO_PATH'] = office.unopath
info(3, "-> Switching from %s to %s" % (sys.executable, office.python))
if os.name in ('nt', 'os2'):
# os.execv is broken on Windows and can't properly parse command line
# arguments and executable name if they contain whitespaces. subprocess
# fixes that behavior.
ret = subprocess.call([office.python, ] + sys.argv[0:])
sys.exit(ret)
else:
# Set LD_LIBRARY_PATH so that "import pyuno" finds libpyuno.so:
if 'LD_LIBRARY_PATH' in os.environ:
os.environ['LD_LIBRARY_PATH'] = office.unopath + os.pathsep + \
realpath(office.urepath, 'lib') + os.pathsep + \
os.environ['LD_LIBRARY_PATH']
else:
os.environ['LD_LIBRARY_PATH'] = office.unopath + os.pathsep + \
realpath(office.urepath, 'lib')
try:
os.execvpe(office.python, [office.python, ] + sys.argv[0:], os.environ)
except OSError:
# Mac OS X versions prior to 10.6 do not support execv in
# a process that contains multiple threads. Instead of
# re-executing in the current process, start a new one
# and cause the current process to exit. This isn't
# ideal since the new process is detached from the parent
# terminal and thus cannot easily be killed with ctrl-C,
# but it's better than not being able to autoreload at
# all.
# Unfortunately the errno returned in this case does not
# appear to be consistent, so we can't easily check for
# this error specifically.
ret = os.spawnvpe(os.P_WAIT, office.python, [office.python, ] + sys.argv[0:], os.environ)
if ret != 0:
error("Switching Python to %s failed." % (office.python))
sys.exit(ret)
class Fmt:
def __init__(self, doctype, name, extension, summary, filter):
self.doctype = doctype
self.name = name
self.extension = extension
self.summary = summary
self.filter = filter
def __str__(self):
return "%s [.%s]" % (self.summary, self.extension)
def __repr__(self):
return "%s/%s" % (self.name, self.doctype)
class FmtList:
def __init__(self):
self.list = []
def add(self, doctype, name, extension, summary, filter):
self.list.append(Fmt(doctype, name, extension, summary, filter))
def byname(self, name):
ret = []
for fmt in self.list:
if fmt.name == name:
ret.append(fmt)
return ret
def byextension(self, extension):
ret = []
for fmt in self.list:
if os.extsep + fmt.extension == extension:
ret.append(fmt)
return ret
def bydoctype(self, doctype, name):
ret = []
for fmt in self.list:
if fmt.name == name and fmt.doctype == doctype:
ret.append(fmt)
return ret
def display(self, doctype):
print("The following list of %s formats are currently available:\n" % doctype, file=sys.stderr)
for fmt in self.list:
if fmt.doctype == doctype:
print(" %-8s - %s" % (fmt.name, fmt), file=sys.stderr)
print(file=sys.stderr)
fmts = FmtList()
# TextDocument
fmts.add('document', 'bib', 'bib', 'BibTeX', 'BibTeX_Writer') # 22
fmts.add('document', 'doc', 'doc', 'Microsoft Word 97/2000/XP', 'MS Word 97') # 29
fmts.add('document', 'doc6', 'doc', 'Microsoft Word 6.0', 'MS WinWord 6.0') # 24
fmts.add('document', 'doc95', 'doc', 'Microsoft Word 95', 'MS Word 95') # 28
fmts.add('document', 'docbook', 'xml', 'DocBook', 'DocBook File') # 39
fmts.add('document', 'docx', 'docx', 'Microsoft Office Open XML', 'Office Open XML Text')
fmts.add('document', 'docx7', 'docx', 'Microsoft Office Open XML', 'MS Word 2007 XML')
fmts.add('document', 'fodt', 'fodt', 'OpenDocument Text (Flat XML)', 'OpenDocument Text Flat XML')
fmts.add('document', 'html', 'html', 'HTML Document (OpenOffice.org Writer)', 'HTML (StarWriter)') # 3
fmts.add('document', 'latex', 'ltx', 'LaTeX 2e', 'LaTeX_Writer') # 31
fmts.add('document', 'mediawiki', 'txt', 'MediaWiki', 'MediaWiki')
fmts.add('document', 'odt', 'odt', 'ODF Text Document', 'writer8') # 10
fmts.add('document', 'ooxml', 'xml', 'Microsoft Office Open XML', 'MS Word 2003 XML') # 11
fmts.add('document', 'ott', 'ott', 'Open Document Text', 'writer8_template') # 21
fmts.add('document', 'pdb', 'pdb', 'AportisDoc (Palm)', 'AportisDoc Palm DB')
fmts.add('document', 'pdf', 'pdf', 'Portable Document Format', 'writer_pdf_Export') # 18
fmts.add('document', 'psw', 'psw', 'Pocket Word', 'PocketWord File')
fmts.add('document', 'rtf', 'rtf', 'Rich Text Format', 'Rich Text Format') # 16
fmts.add('document', 'sdw', 'sdw', 'StarWriter 5.0', 'StarWriter 5.0') # 23
fmts.add('document', 'sdw4', 'sdw', 'StarWriter 4.0', 'StarWriter 4.0') # 2
fmts.add('document', 'sdw3', 'sdw', 'StarWriter 3.0', 'StarWriter 3.0') # 20
fmts.add('document', 'stw', 'stw', 'Open Office.org 1.0 Text Document Template', 'writer_StarOffice_XML_Writer_Template') # 9
fmts.add('document', 'sxw', 'sxw', 'Open Office.org 1.0 Text Document', 'StarOffice XML (Writer)') # 1
fmts.add('document', 'text', 'txt', 'Text Encoded', 'Text (encoded)') # 26
fmts.add('document', 'txt', 'txt', 'Text', 'Text') # 34
fmts.add('document', 'uot', 'uot', 'Unified Office Format text', 'UOF text') # 27
fmts.add('document', 'vor', 'vor', 'StarWriter 5.0 Template', 'StarWriter 5.0 Vorlage/Template') # 6
fmts.add('document', 'vor4', 'vor', 'StarWriter 4.0 Template', 'StarWriter 4.0 Vorlage/Template') # 5
fmts.add('document', 'vor3', 'vor', 'StarWriter 3.0 Template', 'StarWriter 3.0 Vorlage/Template') # 4
fmts.add('document', 'wps', 'wps', 'Microsoft Works', 'MS_Works')
fmts.add('document', 'xhtml', 'html', 'XHTML Document', 'XHTML Writer File') # 33
fmts.add('document', 'epub', 'epub', 'Electronic Publication', 'EPUB')
fmts.add('document', 'png', 'png', 'Portable Network Graphic', 'writer_png_Export') ### 2
# WebDocument
fmts.add('web', 'etext', 'txt', 'Text Encoded (OpenOffice.org Writer/Web)', 'Text (encoded) (StarWriter/Web)') # 14
fmts.add('web', 'html10', 'html', 'OpenOffice.org 1.0 HTML Template', 'writer_web_StarOffice_XML_Writer_Web_Template') # 11
fmts.add('web', 'html', 'html', 'HTML Document', 'HTML') # 2
fmts.add('web', 'html', 'html', 'HTML Document Template', 'writerweb8_writer_template') # 13
fmts.add('web', 'mediawiki', 'txt', 'MediaWiki', 'MediaWiki_Web') # 9
fmts.add('web', 'pdf', 'pdf', 'PDF - Portable Document Format', 'writer_web_pdf_Export') # 10
fmts.add('web', 'sdw3', 'sdw', 'StarWriter 3.0 (OpenOffice.org Writer/Web)', 'StarWriter 3.0 (StarWriter/Web)') # 3
fmts.add('web', 'sdw4', 'sdw', 'StarWriter 4.0 (OpenOffice.org Writer/Web)', 'StarWriter 4.0 (StarWriter/Web)') # 4
fmts.add('web', 'sdw', 'sdw', 'StarWriter 5.0 (OpenOffice.org Writer/Web)', 'StarWriter 5.0 (StarWriter/Web)') # 5
fmts.add('web', 'txt', 'txt', 'OpenOffice.org Text (OpenOffice.org Writer/Web)', 'writerweb8_writer') # 12
fmts.add('web', 'text10', 'txt', 'OpenOffice.org 1.0 Text Document (OpenOffice.org Writer/Web)', 'writer_web_StarOffice_XML_Writer') # 15
fmts.add('web', 'text', 'txt', 'Text (OpenOffice.org Writer/Web)', 'Text (StarWriter/Web)') # 8
fmts.add('web', 'vor4', 'vor', 'StarWriter/Web 4.0 Template', 'StarWriter/Web 4.0 Vorlage/Template') # 6
fmts.add('web', 'vor', 'vor', 'StarWriter/Web 5.0 Template', 'StarWriter/Web 5.0 Vorlage/Template') # 7
# Spreadsheet
fmts.add('spreadsheet', 'csv', 'csv', 'Text CSV', 'Text - txt - csv (StarCalc)') # 16
fmts.add('spreadsheet', 'dbf', 'dbf', 'dBASE', 'dBase') # 22
fmts.add('spreadsheet', 'dif', 'dif', 'Data Interchange Format', 'DIF') # 5
fmts.add('spreadsheet', 'fods', 'fods', 'OpenDocument Spreadsheet (Flat XML)', 'OpenDocument Spreadsheet Flat XML')
fmts.add('spreadsheet', 'html', 'html', 'HTML Document (OpenOffice.org Calc)', 'HTML (StarCalc)') # 7
fmts.add('spreadsheet', 'ods', 'ods', 'ODF Spreadsheet', 'calc8') # 15
fmts.add('spreadsheet', 'ooxml', 'xml', 'Microsoft Excel 2003 XML', 'MS Excel 2003 XML') # 23
fmts.add('spreadsheet', 'ots', 'ots', 'ODF Spreadsheet Template', 'calc8_template') # 14
fmts.add('spreadsheet', 'pdf', 'pdf', 'Portable Document Format', 'calc_pdf_Export') # 34
fmts.add('spreadsheet', 'pxl', 'pxl', 'Pocket Excel', 'Pocket Excel')
fmts.add('spreadsheet', 'sdc', 'sdc', 'StarCalc 5.0', 'StarCalc 5.0') # 31
fmts.add('spreadsheet', 'sdc4', 'sdc', 'StarCalc 4.0', 'StarCalc 4.0') # 11
fmts.add('spreadsheet', 'sdc3', 'sdc', 'StarCalc 3.0', 'StarCalc 3.0') # 29
fmts.add('spreadsheet', 'slk', 'slk', 'SYLK', 'SYLK') # 35
fmts.add('spreadsheet', 'stc', 'stc', 'OpenOffice.org 1.0 Spreadsheet Template', 'calc_StarOffice_XML_Calc_Template') # 2
fmts.add('spreadsheet', 'sxc', 'sxc', 'OpenOffice.org 1.0 Spreadsheet', 'StarOffice XML (Calc)') # 3
fmts.add('spreadsheet', 'uos', 'uos', 'Unified Office Format spreadsheet', 'UOF spreadsheet') # 9
fmts.add('spreadsheet', 'vor3', 'vor', 'StarCalc 3.0 Template', 'StarCalc 3.0 Vorlage/Template') # 18
fmts.add('spreadsheet', 'vor4', 'vor', 'StarCalc 4.0 Template', 'StarCalc 4.0 Vorlage/Template') # 19
fmts.add('spreadsheet', 'vor', 'vor', 'StarCalc 5.0 Template', 'StarCalc 5.0 Vorlage/Template') # 20
fmts.add('spreadsheet', 'xhtml', 'xhtml', 'XHTML', 'XHTML Calc File') # 26
fmts.add('spreadsheet', 'xls', 'xls', 'Microsoft Excel 97/2000/XP', 'MS Excel 97') # 12
fmts.add('spreadsheet', 'xls5', 'xls', 'Microsoft Excel 5.0', 'MS Excel 5.0/95') # 8
fmts.add('spreadsheet', 'xls95', 'xls', 'Microsoft Excel 95', 'MS Excel 95') # 10
fmts.add('spreadsheet', 'xlt', 'xlt', 'Microsoft Excel 97/2000/XP Template', 'MS Excel 97 Vorlage/Template') # 6
fmts.add('spreadsheet', 'xlt5', 'xlt', 'Microsoft Excel 5.0 Template', 'MS Excel 5.0/95 Vorlage/Template') # 28
fmts.add('spreadsheet', 'xlt95', 'xlt', 'Microsoft Excel 95 Template', 'MS Excel 95 Vorlage/Template') # 21
fmts.add('spreadsheet', 'xlsx', 'xlsx', 'Microsoft Excel 2007/2010 XML', 'Calc MS Excel 2007 XML')
# Graphics
fmts.add('graphics', 'bmp', 'bmp', 'Windows Bitmap', 'draw_bmp_Export') # 21
fmts.add('graphics', 'emf', 'emf', 'Enhanced Metafile', 'draw_emf_Export') # 15
fmts.add('graphics', 'eps', 'eps', 'Encapsulated PostScript', 'draw_eps_Export') # 48
fmts.add('graphics', 'fodg', 'fodg', 'OpenDocument Drawing (Flat XML)', 'OpenDocument Drawing Flat XML')
fmts.add('graphics', 'gif', 'gif', 'Graphics Interchange Format', 'draw_gif_Export') # 30
fmts.add('graphics', 'html', 'html', 'HTML Document (OpenOffice.org Draw)', 'draw_html_Export') # 37
fmts.add('graphics', 'jpg', 'jpg', 'Joint Photographic Experts Group', 'draw_jpg_Export') # 3
fmts.add('graphics', 'jpeg', 'jpeg', 'Joint Photographic Experts Group', 'draw_jpg_Export') # 3
fmts.add('graphics', 'met', 'met', 'OS/2 Metafile', 'draw_met_Export') # 43
fmts.add('graphics', 'odd', 'odd', 'OpenDocument Drawing', 'draw8') # 6
fmts.add('graphics', 'otg', 'otg', 'OpenDocument Drawing Template', 'draw8_template') # 20
fmts.add('graphics', 'pbm', 'pbm', 'Portable Bitmap', 'draw_pbm_Export') # 14
fmts.add('graphics', 'pct', 'pct', 'Mac Pict', 'draw_pct_Export') # 41
fmts.add('graphics', 'pdf', 'pdf', 'Portable Document Format', 'draw_pdf_Export') # 28
fmts.add('graphics', 'pgm', 'pgm', 'Portable Graymap', 'draw_pgm_Export') # 11
fmts.add('graphics', 'png', 'png', 'Portable Network Graphic', 'draw_png_Export') # 2
fmts.add('graphics', 'ppm', 'ppm', 'Portable Pixelmap', 'draw_ppm_Export') # 5
fmts.add('graphics', 'ras', 'ras', 'Sun Raster Image', 'draw_ras_Export') # 31
fmts.add('graphics', 'std', 'std', 'OpenOffice.org 1.0 Drawing Template', 'draw_StarOffice_XML_Draw_Template') # 53
fmts.add('graphics', 'svg', 'svg', 'Scalable Vector Graphics', 'draw_svg_Export') # 50
fmts.add('graphics', 'svm', 'svm', 'StarView Metafile', 'draw_svm_Export') # 55
fmts.add('graphics', 'swf', 'swf', 'Macromedia Flash (SWF)', 'draw_flash_Export') # 23
fmts.add('graphics', 'sxd', 'sxd', 'OpenOffice.org 1.0 Drawing', 'StarOffice XML (Draw)') # 26
fmts.add('graphics', 'sxd3', 'sxd', 'StarDraw 3.0', 'StarDraw 3.0') # 40
fmts.add('graphics', 'sxd5', 'sxd', 'StarDraw 5.0', 'StarDraw 5.0') # 44
fmts.add('graphics', 'sxw', 'sxw', 'StarOffice XML (Draw)', 'StarOffice XML (Draw)')
fmts.add('graphics', 'tiff', 'tiff', 'Tagged Image File Format', 'draw_tif_Export') # 13
fmts.add('graphics', 'vor', 'vor', 'StarDraw 5.0 Template', 'StarDraw 5.0 Vorlage') # 36
fmts.add('graphics', 'vor3', 'vor', 'StarDraw 3.0 Template', 'StarDraw 3.0 Vorlage') # 35
fmts.add('graphics', 'wmf', 'wmf', 'Windows Metafile', 'draw_wmf_Export') # 8
fmts.add('graphics', 'xhtml', 'xhtml', 'XHTML', 'XHTML Draw File') # 45
fmts.add('graphics', 'xpm', 'xpm', 'X PixMap', 'draw_xpm_Export') # 19
# Presentation
fmts.add('presentation', 'bmp', 'bmp', 'Windows Bitmap', 'impress_bmp_Export') # 15
fmts.add('presentation', 'emf', 'emf', 'Enhanced Metafile', 'impress_emf_Export') # 16
fmts.add('presentation', 'eps', 'eps', 'Encapsulated PostScript', 'impress_eps_Export') # 17
fmts.add('presentation', 'fodp', 'fodp', 'OpenDocument Presentation (Flat XML)', 'OpenDocument Presentation Flat XML')
fmts.add('presentation', 'gif', 'gif', 'Graphics Interchange Format', 'impress_gif_Export') # 18
fmts.add('presentation', 'html', 'html', 'HTML Document (OpenOffice.org Impress)', 'impress_html_Export') # 43
fmts.add('presentation', 'jpg', 'jpg', 'Joint Photographic Experts Group', 'impress_jpg_Export') # 19
fmts.add('presentation', 'met', 'met', 'OS/2 Metafile', 'impress_met_Export') # 20
fmts.add('presentation', 'odg', 'odg', 'ODF Drawing (Impress)', 'impress8_draw') # 29
fmts.add('presentation', 'odp', 'odp', 'ODF Presentation', 'impress8') # 9
fmts.add('presentation', 'otp', 'otp', 'ODF Presentation Template', 'impress8_template') # 38
fmts.add('presentation', 'pbm', 'pbm', 'Portable Bitmap', 'impress_pbm_Export') # 21
fmts.add('presentation', 'pct', 'pct', 'Mac Pict', 'impress_pct_Export') # 22
fmts.add('presentation', 'pdf', 'pdf', 'Portable Document Format', 'impress_pdf_Export') # 23
fmts.add('presentation', 'pgm', 'pgm', 'Portable Graymap', 'impress_pgm_Export') # 24
fmts.add('presentation', 'png', 'png', 'Portable Network Graphic', 'impress_png_Export') # 25
fmts.add('presentation', 'potm', 'potm', 'Microsoft PowerPoint 2007/2010 XML Template', 'Impress MS PowerPoint 2007 XML Template')
fmts.add('presentation', 'pot', 'pot', 'Microsoft PowerPoint 97/2000/XP Template', 'MS PowerPoint 97 Vorlage') # 3
fmts.add('presentation', 'ppm', 'ppm', 'Portable Pixelmap', 'impress_ppm_Export') # 26
fmts.add('presentation', 'pptx', 'pptx', 'Microsoft PowerPoint 2007/2010 XML', 'Impress MS PowerPoint 2007 XML') # 36
fmts.add('presentation', 'pps', 'pps', 'Microsoft PowerPoint 97/2000/XP (Autoplay)', 'MS PowerPoint 97 Autoplay') # 36
fmts.add('presentation', 'ppt', 'ppt', 'Microsoft PowerPoint 97/2000/XP', 'MS PowerPoint 97') # 36
fmts.add('presentation', 'pwp', 'pwp', 'PlaceWare', 'placeware_Export') # 30
fmts.add('presentation', 'ras', 'ras', 'Sun Raster Image', 'impress_ras_Export') # 27
fmts.add('presentation', 'sda', 'sda', 'StarDraw 5.0 (OpenOffice.org Impress)', 'StarDraw 5.0 (StarImpress)') # 8
fmts.add('presentation', 'sdd', 'sdd', 'StarImpress 5.0', 'StarImpress 5.0') # 6
fmts.add('presentation', 'sdd3', 'sdd', 'StarDraw 3.0 (OpenOffice.org Impress)', 'StarDraw 3.0 (StarImpress)') # 42
fmts.add('presentation', 'sdd4', 'sdd', 'StarImpress 4.0', 'StarImpress 4.0') # 37
fmts.add('presentation', 'sxd', 'sxd', 'OpenOffice.org 1.0 Drawing (OpenOffice.org Impress)', 'impress_StarOffice_XML_Draw') # 31
fmts.add('presentation', 'sti', 'sti', 'OpenOffice.org 1.0 Presentation Template', 'impress_StarOffice_XML_Impress_Template') # 5
fmts.add('presentation', 'svg', 'svg', 'Scalable Vector Graphics', 'impress_svg_Export') # 14
fmts.add('presentation', 'svm', 'svm', 'StarView Metafile', 'impress_svm_Export') # 13
fmts.add('presentation', 'swf', 'swf', 'Macromedia Flash (SWF)', 'impress_flash_Export') # 34
fmts.add('presentation', 'sxi', 'sxi', 'OpenOffice.org 1.0 Presentation', 'StarOffice XML (Impress)') # 41
fmts.add('presentation', 'tiff', 'tiff', 'Tagged Image File Format', 'impress_tif_Export') # 12
fmts.add('presentation', 'uop', 'uop', 'Unified Office Format presentation', 'UOF presentation') # 4
fmts.add('presentation', 'vor', 'vor', 'StarImpress 5.0 Template', 'StarImpress 5.0 Vorlage') # 40
fmts.add('presentation', 'vor3', 'vor', 'StarDraw 3.0 Template (OpenOffice.org Impress)', 'StarDraw 3.0 Vorlage (StarImpress)') # 1
fmts.add('presentation', 'vor4', 'vor', 'StarImpress 4.0 Template', 'StarImpress 4.0 Vorlage') # 39
fmts.add('presentation', 'vor5', 'vor', 'StarDraw 5.0 Template (OpenOffice.org Impress)', 'StarDraw 5.0 Vorlage (StarImpress)') # 2
fmts.add('presentation', 'wmf', 'wmf', 'Windows Metafile', 'impress_wmf_Export') # 11
fmts.add('presentation', 'xhtml', 'xml', 'XHTML', 'XHTML Impress File') # 33
fmts.add('presentation', 'xpm', 'xpm', 'X PixMap', 'impress_xpm_Export') # 10
class Options:
def __init__(self, args):
self.connection = None
self.debug = False
self.doctype = None
self.exportfilter = []
self.exportfilteroptions = ""
self.fields = {}
self.filenames = []
self.format = None
self.importfilter = []
self.importfiltername = None
self.importfilteroptions = ""
self.listener = False
self.metadata = {}
self.nolaunch = False
self.output = None
self.paperformat = None
self.paperorientation = None
self.papersize = None
self.password = None
self.pipe = None
self.port = '2002'
self.preserve = False
self.server = '127.0.0.1'
self.setprinter = False
self.showlist = False
self.stdin = False
self.stdout = False
self.template = None
self.timeout = 60
self.verbose = 0
self.userProfile = None
self.updateDocMode = NO_UPDATE
self.updatehtmllinks = True
# Get options from the commandline
try:
opts, args = getopt.getopt(args, 'c:Dd:e:F:f:hi:I:LlM:no:p:s:T:t:P:vV',
['disable-html-update-links', 'connection=', 'debug', 'doctype=', 'export=', 'field=', 'format=',
'help', 'import=', 'import-filter-name=', 'listener', 'meta=', 'no-launch',
'output=', 'outputpath', 'password=', 'pipe=', 'port=', 'preserve',
'server=', 'timeout=', 'user-profile=', 'show', 'stdin',
'stdout', 'template', 'printer=', 'unsafe-quiet-update', 'verbose', 'version'])
except getopt.error as exc:
print('unoconv: %s, try unoconv -h for a list of all the options' % str(exc))
sys.exit(255)
for opt, arg in opts:
if opt in ['-h', '--help']:
self.usage()
print()
self.help()
sys.exit(0)
elif opt in ['-c', '--connection']:
self.connection = arg
elif opt in ['--debug']:
self.debug = True
elif opt in ['-d', '--doctype']:
self.doctype = arg
elif opt in ['-e', '--export']:
l = arg.split('=')
if len(l) == 2:
(name, value) = l
if name in ('FilterOptions'):
self.exportfilteroptions = value
elif value in ('True', 'true'):
self.exportfilter.append(PropertyValue(name, 0, True, 0))
elif value in ('False', 'false'):
self.exportfilter.append(PropertyValue(name, 0, False, 0))
else:
try:
self.exportfilter.append(PropertyValue(name, 0, int(value), 0))
except ValueError:
self.exportfilter.append(PropertyValue(name, 0, value, 0))
else:
print('Warning: Option %s cannot be parsed, ignoring.' % arg, file=sys.stderr)
elif opt in ['-F', '--field']:
l = arg.split('=')
self.fields[l[0]] = '='.join(l[1:])
elif opt in ['-f', '--format']:
self.format = arg
elif opt in ['-i', '--import']:
l = arg.split('=')
if len(l) == 2:
(name, value) = l
if name in ('FilterOptions'):
self.importfilteroptions = value
elif value in ('True', 'true'):
self.importfilter.append(PropertyValue(name, 0, True, 0))
elif value in ('False', 'false'):
self.importfilter.append(PropertyValue(name, 0, False, 0))
else:
try:
self.importfilter.append(PropertyValue(name, 0, int(value), 0))
except ValueError:
self.importfilter.append(PropertyValue(name, 0, value, 0))
else:
print('Warning: Option %s cannot be parsed, ignoring.' % arg, file=sys.stderr)
elif opt in ['-I', '--import-filter-name']:
self.importfiltername = arg
elif opt in ['-l', '--listener']:
self.listener = True
elif opt in ['-M', '--meta']:
l = arg.split('=')
self.metadata[l[0]] = '='.join(l[1:])
elif opt in ['-n', '--no-launch']:
self.nolaunch = True
elif opt in ['-o', '--output']:
self.output = arg
elif opt in ['--outputpath']:
print('Warning: This option is deprecated by --output.', file=sys.stderr)
self.output = arg
elif opt in ['--password']:
self.password = arg
elif opt in ['--pipe']:
self.pipe = arg
elif opt in ['-p', '--port']:
self.port = arg
elif opt in ['--preserve']:
self.preserve = True
elif opt in ['-s', '--server']:
self.server = arg
elif opt in ['--show']:
self.showlist = True
elif opt in ['--stdin']:
self.stdin = True
elif opt in ['--stdout']:
self.stdout = True
elif opt in ['-t', '--template']:
self.template = arg
elif opt in ['--disable-html-update-links']:
self.updatehtmllinks = False
elif opt in ['-T', '--timeout']:
self.timeout = int(arg)
elif opt in ['--unsafe-quiet-update']:
# ref https://www.openoffice.org/api/docs/common/ref/com/sun/star/document/UpdateDocMode.html
print('Warning: Do not use the option --unsafe-quiet-update with untrusted input.')
self.updateDocMode = QUIET_UPDATE
elif opt in ['-v', '--verbose']:
self.verbose = self.verbose + 1
elif opt in ['-V', '--version']:
self.version()
sys.exit(0)
elif opt in ['-P', '--printer']:
optKey, optValue = arg.split('=')
if optKey in ['PaperFormat']:
self.paperformat = optValue
self.setprinter = True
elif optKey in ['PaperOrientation']:
self.paperorientation = optValue.upper()
self.setprinter = True
elif optKey in ['PaperSize']:
intFunc = int if sys.version_info.major > 2 else long
size = list(map(lambda s: intFunc(s), optValue.split('x')))
if (2 == len(size)):
self.papersize = size
self.setprinter = True
elif opt in ['--user-profile']:
self.userProfile = arg
# Enable verbosity
if self.verbose >= 2:
print('Verbosity set to level %d' % self.verbose, file=sys.stderr)
self.filenames = args
if not self.listener and not self.showlist and not self.stdin and self.doctype != 'list' and not self.filenames:
print('unoconv: you have to provide a filename or url as argument', file=sys.stderr)
print('Try `unoconv -h\' for more information.', file=sys.stderr)
sys.exit(255)
# Set connection string
if not self.connection:
if not self.pipe:
self.connection = "socket,host=%s,port=%s,tcpNoDelay=1;urp;StarOffice.ComponentContext" % (self.server, self.port)
else:
self.connection = "pipe,name=%s;urp;StarOffice.ComponentContext" % (self.pipe)
# Make it easier for people to use a doctype (first letter is enough)
if self.doctype:
for doctype in doctypes:
if doctype.startswith(self.doctype):
self.doctype = doctype
# Check if the user request to see the list of formats
if self.showlist or self.format == 'list':
if self.doctype:
fmts.display(self.doctype)
else:
for t in doctypes:
fmts.display(t)
sys.exit(0)
# If no format was specified, probe it or provide it.
if not self.format:
# Check if the command is in the form odt2pdf
l = sys.argv[0].split('2')
if len(l) == 2:
self.format = l[1]
# Use the extension of the output file
elif self.output and os.path.basename(self.output).find('.') >= 0:
self.format = os.path.splitext(self.output)[1].lstrip('.')
# Default to PDF.
if not self.format:
self.format = 'pdf'
def version(self):
print('unoconv %s' % __version__)
print('Written by Dag Wieers <[email protected]>')
print('Homepage at http://dag.wieers.com/home-made/unoconv/')
print()
print('platform %s/%s' % (os.name, sys.platform))
print('python %s' % sys.version)
if uno:
# Get office product information.
product = uno.getComponentContext().ServiceManager.createInstance("com.sun.star.configuration.ConfigurationProvider").createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", UnoProps(nodepath="/org.openoffice.Setup/Product"))
print(product.ooName, product.ooSetupVersionAboutBox)
def usage(self):
print('usage: unoconv [options] file [file2 ..]', file=sys.stderr)
def help(self):
print('''Convert from and to any format supported by LibreOffice
unoconv options:
-c, --connection=string use a custom connection string
-d, --doctype=type specify document type
(document, graphics, presentation, spreadsheet)
-e, --export=name=value set export filter options
eg. -e PageRange=1-2
-f, --format=format specify the output format
-F, --field=name=value replace user-defined text field with value
eg. -F Client_Name="Oracle"
-i, --import=string set import filter option string
eg. -i utf8
-I, --import-filter-name=string set import filter name, useful when converting stdin
or files without an extension)
eg. -I ooxml
-l, --listener start a permanent listener to use by unoconv clients
-n, --no-launch fail if no listener is found (default: launch one)
-o, --output=name output basename, filename or directory
--pipe=name alternative method of connection using a pipe
-p, --port=port specify the port (default: 2002)
to be used by client or listener
--password=string provide a password to decrypt the document
--preserve keep timestamp and permissions of the original document
-s, --server=server specify the server address (default: 127.0.0.1)
to be used by client or listener
--show list the available output formats
--stdin read from stdin (filenames are ignored if provided)
--stdout write output to stdout
-t, --template=file import the styles from template (.ott)
-T, --timeout=secs timeout after secs if connection to listener fails
--unsafe-quiet-update allow rendered document to fetch external resources (Warning: this is unsafe with untrusted input)
-v, --verbose be more and more verbose (-vvv for debugging)
--version display version number of unoconv, OOo/LO and platform details
-P, --printer=name=value printer options
PaperFormat: specify printer paper format
eg. -P PaperFormat=A3
PaperOrientation: specify printer paper orientation
eg. -P PaperOrientation=landscape
PaperSize: specify printer paper size, paper format should set to USER, size=widthxheight
eg. -P PaperSize=130x200 means width=130, height=200
--disable-html-update-links disables the recheck for updating links missed by libreoffice
--user-profile=path use a custom user profile path
''', file=sys.stderr)
class Convertor:
def __init__(self):
global exitcode, ooproc, office, product
unocontext = None
# Do the LibreOffice component dance
self.context = uno.getComponentContext()
self.svcmgr = self.context.ServiceManager
resolver = self.svcmgr.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", self.context)
# Test for an existing connection
info(3, 'Connection type: %s' % op.connection)
unocontext = self.connect(resolver)
if not unocontext:
die(251, "Unable to connect or start own listener. Aborting.")
# And some more LibreOffice magic
unosvcmgr = unocontext.ServiceManager
self.desktop = unosvcmgr.createInstanceWithContext("com.sun.star.frame.Desktop", unocontext)
self.cwd = unohelper.systemPathToFileUrl(os.getcwd())
# List all filters
# self.filters = unosvcmgr.createInstanceWithContext("com.sun.star.document.FilterFactory", unocontext)
# for filter in self.filters.getElementNames():
# print filter
# print dir(filter), dir(filter.format)
def connect(self, resolver):
global ooproc, product, office
unocontext = None
try:
unocontext = resolver.resolve("uno:%s" % op.connection)
except NoConnectException as e:
# info(3, "Existing listener not found.\n%s" % e)
info(3, "Existing listener not found.")
if op.nolaunch:
die(113, "Existing listener not found. Unable start listener by parameters. Aborting.")
# Start our own OpenOffice instance
info(3, "Launching our own listener using %s." % office.binary)
try:
product = self.svcmgr.createInstance("com.sun.star.configuration.ConfigurationProvider").createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", UnoProps(nodepath="/org.openoffice.Setup/Product"))
if product.ooName not in ('LibreOffice', 'LOdev') or LooseVersion(product.ooSetupVersion) <= LooseVersion('3.3'):
args = [office.binary, "-headless", "-invisible", "-nocrashreport", "-nodefault", "-nofirststartwizard", "-nologo", "-norestore", "-accept=%s" % op.connection]
else:
args = [office.binary, "--headless", "--invisible", "--nocrashreport", "--nodefault", "--nofirststartwizard", "--nologo", "--norestore", "--accept=%s" % op.connection]
if op.userProfile:
args.append("-env:UserInstallation=file://" + realpath(op.userProfile))
info(2, '%s listener arguments are %s.' % (product.ooName, args))
ooproc = subprocess.Popen(args, env=os.environ)
info(2, '%s listener successfully started. (pid=%s)' % (product.ooName, ooproc.pid))
# Try connection to it for op.timeout seconds (flakky OpenOffice)
timeout = 0
while timeout <= op.timeout:
# Is it already/still running?
retcode = ooproc.poll()
if retcode == 81:
info(3, "Caught exit code 81 (new installation). Restarting listener.")
return self.connect(resolver)
break
elif retcode is not None:
info(3, "Process %s (pid=%s) exited with %s." % (office.binary, ooproc.pid, retcode))
break
try:
unocontext = resolver.resolve("uno:%s" % op.connection)
break
except NoConnectException:
time.sleep(0.5)
timeout += 0.5
except:
raise
else:
error("Failed to connect to %s (pid=%s) in %d seconds.\n%s" % (office.binary, ooproc.pid, op.timeout, e))
except Exception as e:
raise
error("Launch of %s failed.\n%s" % (office.binary, e))
return unocontext
def getimportformat(self):
if op.doctype:
importformat = fmts.bydoctype(op.doctype, op.importfiltername)
else:
importformat = fmts.byname(op.importfiltername)
if not importformat:
error('Import format [%s] is not known to unoconv.' % importformat)
return importformat[0]
def getformat(self, inputfn):
doctype = None
# Get the output format from mapping
if op.doctype:
outputfmt = fmts.bydoctype(op.doctype, op.format)
else:
outputfmt = fmts.byname(op.format)
if not outputfmt:
outputfmt = fmts.byextension(os.extsep + op.format)
# If no doctype given, check list of acceptable formats for input file ext doctype.
# FIXME: This should go into the for-loop to match each individual input filename.
if outputfmt:
inputext = os.path.splitext(inputfn)[1]
inputfmt = fmts.byextension(inputext)
if inputfmt:
for fmt in outputfmt:
if inputfmt[0].doctype == fmt.doctype:
doctype = inputfmt[0].doctype
outputfmt = fmt
break
else:
outputfmt = outputfmt[0]
# print >>sys.stderr, 'Format `%s\' is part of multiple doctypes %s, selecting `%s\'.' % (format, [fmt.doctype for fmt in outputfmt], outputfmt[0].doctype)
else:
outputfmt = outputfmt[0]
# No format found, throw error
if not outputfmt:
if doctype:
error('Format [%s/%s] is not known to unoconv.' % (op.doctype, op.format))
else:
error('Format [%s] is not known to unoconv.' % op.format)
die(1)
return outputfmt
def preserve(self, inputfn, outputfn):
# Get timestamp of input file.
s = os.stat(inputfn)
times = (s.st_atime, s.st_mtime)
mode = s.st_mode
# Set it to output file.
with open(outputfn, "a") as f:
os.utime(f.fileno()
if hasattr(os, "supports_fd") and os.utime in os.supports_fd else inputfn,
times=times)
os.chmod(f.fileno()
if hasattr(os, "supports_fd") and os.chmod in os.supports_fd else inputfn,
mode)
def convert(self, inputfn):
global exitcode
document = None
outputfmt = self.getformat(inputfn)
if op.verbose > 0:
print('Input file:', inputfn, file=sys.stderr)
try:
# Import phase.
phase = "import"
# Load inputfile.
inputprops = UnoProps(Hidden=True, ReadOnly=True, UpdateDocMode=op.updateDocMode)
if op.password:
inputprops += UnoProps(Password=op.password)
# Cannot use UnoProps for FilterData property.
if op.importfilteroptions:
# print "Import filter options: %s" % op.importfilteroptions
inputprops += UnoProps(FilterOptions=op.importfilteroptions)
# Cannot use UnoProps for FilterData property.
if op.importfilter:
inputprops += (PropertyValue("FilterData", 0, uno.Any("[]com.sun.star.beans.PropertyValue", tuple(op.importfilter), ), 0), )