-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
296 lines (236 loc) · 9.06 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Author: Christopher Bull.
# Affiliation: Climate Change Research Centre and ARC Centre of Excellence for Climate System Science.
# Level 4, Mathews Building
# University of New South Wales
# Sydney, NSW, Australia, 2052
# Contact: [email protected]
# www: christopherbull.com.au
# Date created: Fri, 13 Nov 2015 13:45:57
# Machine created on: ccrc165
#
"""
Main script for MkPaper package
"""
from ._cblogger import _LogStart
import os
import contextlib as ctx
import subprocess
_lg=_LogStart().setup()
header=\
r"""
\title{Figures}
\author{
This is an author name \
}
\date{\today}
\documentclass{scrartcl}
\usepackage{caption} % Use for \captionof(*) command
% Set the page margins to accomodate space for captions and page numbers
\usepackage[left=0.9cm,right=0.9cm,bottom=1cm,footskip=1em,includefoot]{geometry}
% Use for 'landscape' environment to position landscape captions properly
\usepackage{pdflscape}
% Setup the new 'pagecommand*' option key-value
\usepackage{etoolbox}
\usepackage{pdfpages}
\makeatletter
\newcommand*{\AM@pagecommandstar}{}
\define@key{pdfpages}{pagecommand*}{\def\AM@pagecommandstar{#1}}
\patchcmd{\AM@output}{\begingroup\AM@pagecommand\endgroup}
{\ifthenelse{\boolean{AM@firstpage}}{\begingroup\AM@pagecommandstar\endgroup}{\begingroup\AM@pagecommand\endgroup}}{}{} % Patch to use new option
\patchcmd{\AM@split@optionsii}{\equal{pagecommand}{\AM@temp}\or}
{\equal{pagecommand}{\AM@temp}\or\equal{pagecommand*}{\AM@temp}\or}{}{}
\makeatother
\begin{document}
\maketitle
\begin{abstract}
These are the figures for the a paper\ldots
\end{abstract}
\section{Figure}
This is a descriptive text about the figures
"""
newfig=\
r"""
% Include a single PDF page
\includepdf[
frame,
scale=0.90,
% Use new 'pagecommand*' to set caption on the first page, include page #
"""
def mkdir(p):
"""make directory of path that is passed"""
try:
os.makedirs(p)
_lg.info("output folder: "+p+ " does not exist, we will make one.")
except OSError as exc: # Python >2.5
import errno
if exc.errno == errno.EEXIST and os.path.isdir(p):
pass
else: raise
class LatexFigureDoc(object):
"""
Class for creating LaTeX Figure Doc.
Keep calling add_figure method as required.
Call end_tex method to complete document
Parameters
----------
:latexfol: folder to put the latex and pdf stuff in. (Will be created if it doesn't exist.) Needs to be full path, not relative.
:figdoctitle: name of figure document title (default is 'mkpaperfigure')
Returns
-------
Notes
-------
Example
--------
>>> figobj=LatexFigureDoc(latexfol,figdoctitle)
>>> figobj.add_figure(fpath,caption)
>>> figobj.add_figure(fpath2,caption2)
>>> figobj.add_figure(fpath3,caption3)
>>> figobj.end_tex()
>>> figobj.build_tex()
#Or more concretely...
>>> import mkpaper as mp
>>> import glob
>>> ifiles=sorted(glob.glob('/path/to/plots/' + '*.pdf' ))
>>> assert(ifiles!=[]),"glob didn't find anything!"
>>> figobj=mp.LatexFigureDoc('/home/nfs/username/examplemkpaper/','testname')
>>> figobj.add_figure(ifiles[0],'fig1')
>>> figobj.add_figure(ifiles[1],'fig2')
>>> figobj.add_figure(ifiles[2],'fig3')
>>> #etc
>>> figobj.end_tex()
>>> figobj.build_tex()
"""
def __init__(self,latexfol,figdoctitle='mkpaperfigure'):
_lg.info("Creating LaTeX file in: " + latexfol)
self.latexfol=latexfol
self.figdoctitle=figdoctitle
self.outname=latexfol+figdoctitle+'.tex'
mkdir(self.latexfol)
with ctx.closing(open(self.outname,'w')) as handle:
handle.write(header)
def add_figure(self,filepath,caption):
"""function to add figure and caption to latex file.
:filepath: this needs to be a pdf file!
:caption: caption for said figure
:returns:
"""
_lg.debug("Adding Figure: " + filepath)
with ctx.closing(open(self.outname,'a')) as handle:
handle.write(newfig)
newfig_intro=r"pagecommand*={\thispagestyle{plain}\null\vfill\captionof{figure}{"
newfig_mid="}}]{"
newfig_outro="}"
handle.write(newfig_intro+caption+newfig_mid+filepath+newfig_outro+"\n")
def end_tex(self):
"""function to add figure and caption to latex file.
:filepath: @todo
:caption: @todo
:returns: @todo
"""
with ctx.closing(open(self.outname,'a')) as handle:
handle.write("\end{document}"+"\n")
def build_tex(self):
"""function to call pdflatex to actually build the pdf file.
:filepath: @todo
:caption: @todo
:returns: @todo
"""
_lg.info("Building pdf file... (requires pdflatex)")
#turned this off as it was crashing..
#currentdir=os.getcwd()
os.chdir(self.latexfol)
pdfcommand=['pdflatex' , self.outname]
FNULL = open(os.devnull, 'w')
retcode = subprocess.call(pdfcommand, stdout=FNULL, stderr=subprocess.STDOUT)
#os.chdir(currentdir)
if os.path.isfile(self.outname[:-3]+'pdf'):
_lg.info("MkPaper SUCCESS, check it out: "+self.outname[:-3]+'pdf')
else:
_lg.info("MkPaper FAIL")
_lg.error("Something went wrong with pdflatex, it hasn't made a figure doc. We won't delete the LaTeX.")
sys.exit("Something went wrong with pdflatex, it hasn't made a figure doc. We won't delete the LaTeX.")
class WordFigureDoc(object):
"""
Class for creating Word Figure Doc.
Keep calling add_figure method as required.
Call end_doc method to complete document
Parameters
----------
:wordfol: folder to put the word doc in. (Will be created if it doesn't exist.) Needs to be full path, not relative.
:figdoctitle: name of figure document title (default is 'mkpaperfigure')
:figparagraph: opening paragraph that describes the purpose of the document (default is 'This doc is a figure container')
Returns
-------
Notes
-------
Example
--------
>>> figobj=WordFigureDoc(latexfol,figdoctitle)
>>> figobj.add_figure(fpath,caption)
>>> figobj.add_figure(fpath2,caption2,unicodeme=True)
>>> figobj.end_doc()
#Or more concretely...
>>> import mkpaper as mp
>>> import glob
>>> #get a list of your figures in PNG format...
>>> ifiles=sorted(glob.glob('/path/to/plots/' + '*.png' ))
>>> assert(ifiles!=[]),"glob didn't find anything!"
>>> figobj=mp.WordFigureDoc('/home/nfs/username/examplemkpaper/','testname')
>>> figobj.add_figure(ifiles[0],'fig1')
>>> figobj.add_figure(ifiles[1],'fig2')
>>> figobj.add_figure(ifiles[2],'fig3')
>>> #etc
>>> #insert the tail of the doc
>>> figobj.end_doc()
"""
def __init__(self,wordfol,figdoctitle='mkpaperfigure',figparagraph=''):
_lg.info("Creating word doc file in: " + wordfol)
self.wordfol=wordfol
self.figdoctitle=figdoctitle
self.outname=wordfol+figdoctitle+'.docx'
mkdir(self.wordfol)
from docx import Document
self.document=Document()
if figparagraph=='':
self.document.add_paragraph("This doc is a figure container")
else:
self.document.add_paragraph(figparagraph)
self.document.add_heading("Figure File", 0)
def add_figure(self,filepath,caption,unicodeme=False,nopic=False,manypics=False):
"""function to add figure and caption to word doc file.
:filepath: this needs to be a pdf file!
:caption: caption for said figure
:unicodeme: (optional) set to True if caption contains funky characters
:nopic: (optional) set to True if you want just a caption and no picture
:manypics: (optional) set to True if you want multiple pictures in the one figure
:returns:
Notes
-------
Insert this at the start of your python file to have funky strings..
# coding=utf-8
Discussion:
http://stackoverflow.com/questions/3215168/how-to-get-charater-in-a-string-in-python
"""
from docx.shared import Inches
_lg.debug("Adding Figure: " + filepath)
if not nopic:
self.document.add_picture(filepath,width=Inches(6.0))
if manypics:
return
if unicodeme:
self.document.add_paragraph(unicode(caption,'utf-8'))
else:
self.document.add_paragraph(caption)
if not nopic:
self.document.add_page_break()
return
def end_doc(self):
self.document.save(self.outname)
if os.path.isfile(self.outname):
_lg.info("MkPaper SUCCESS, check it out: "+self.outname)
else:
_lg.info("MkPaper FAIL")
_lg.error("Something went wrong with python-docx, it hasn't made a figure doc.")
sys.exit("Something went wrong with python-docx, it hasn't made a figure doc.")
if __name__ == "__main__":
pass