-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipage_export.py
179 lines (141 loc) · 4.07 KB
/
multipage_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Inkscape extension to export objects as single files
"""
import os
import inkex
import tempfile
import subprocess
import lxml.etree as et
class MultipageExport(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument(
'-n', '--name',
type=str,
dest='name',
help='Base name'
)
self.arg_parser.add_argument(
'-e', '--exporter',
type=str,
dest='exporter',
help='Exporter'
)
self.arg_parser.add_argument(
'-f', '--format',
type=str,
dest='format',
help='File format'
)
self.arg_parser.add_argument(
'-a', '--autoname',
type=str,
dest='autoname',
help='Use IDs for names'
)
self.arg_parser.add_argument(
'-r', '--replace',
type=str,
dest='replace',
help='Replace existing files'
)
self.arg_parser.add_argument(
'-c', '--combine',
type=str,
dest='combine',
help='Combine into single PDF'
)
def run_command(self, args):
try:
with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
proc.wait(timeout=300)
except OSError:
inkex.errormsg('Program "%s" is not installed!' % args[0])
exit()
def write_tempfile(self):
desc, tmpfile = tempfile.mkstemp(suffix='.svg', prefix='multipage-export-')
file = os.fdopen(desc, 'wb')
self.document.write(file)
file.close()
return tmpfile
def export_path(self, fformat):
name = self.options.name
home = os.path.expanduser('~')
path = "%s/Documents/Exports/%s/%s" % (home, name, fformat.upper())
if not os.path.exists(path):
os.makedirs(path)
return path
def file_path(self, fname, fformat):
path = self.export_path(fformat)
path = "%s/%s.%s" % (path, fname, fformat)
return path
def remove_pt(self, path, fformat):
if fformat == 'svg':
xml = et.parse(path)
svg = xml.getroot()
svg.attrib['width'] = svg.attrib['width'].replace('pt', '')
svg.attrib['height'] = svg.attrib['height'].replace('pt', '')
xml.write(path)
def inkscape_export(self, oid, fformat, output, tmpfile):
self.run_command([
'inkscape',
'--vacuum-defs',
'--export-id=%s' % oid,
'--export-id-only',
'--export-filename=%s' % output,
tmpfile
])
def rsvg_export(self, oid, fformat, output, tmpfile):
self.run_command([
'rsvg-convert',
'-u',
'-i', oid,
'-d', '96',
'-p', '96',
'-f', fformat,
'-o', output,
tmpfile
])
def export(self, oid, fformat, output, tmpfile):
if self.options.exporter == 'inkscape':
self.inkscape_export(oid, fformat, output, tmpfile)
else:
self.rsvg_export(oid, fformat, output, tmpfile)
def pdftk_combine(self, output, file_paths):
output = self.file_path("%s-%s" % (output, 'All'), 'pdf')
command = ['pdftk']
command.extend(file_paths)
command.extend(['cat', 'output', output])
self.run_command(command)
def export_files(self):
tmpfile = self.write_tempfile()
fformat = self.options.format
fname = self.options.name
findex = 0
fpaths = []
for oid in self.options.ids:
findex = findex + 1
if self.options.autoname == 'true':
output = self.file_path(oid, fformat)
else:
output = self.file_path("%s-%s" % (fname, findex), fformat)
fpaths.append(output)
if self.options.replace == 'false' and os.path.exists(output):
continue
self.export(oid, fformat, output, tmpfile)
self.remove_pt(output, fformat)
if self.options.combine == 'true' and fformat == 'pdf':
self.pdftk_combine(fname, fpaths)
def effect(self):
if self.options.name is None:
inkex.errormsg('Please enter the folder name.')
exit()
if len(self.options.ids) < 1:
inkex.errormsg('Please select at least 1 object.')
exit()
self.export_files()
if __name__ == '__main__':
exporter = MultipageExport()
exporter.run()