Skip to content

Instantly share code, notes, and snippets.

@williamgilpin
Created May 1, 2015 21:16
Show Gist options
  • Save williamgilpin/014caa2f613c94633a49 to your computer and use it in GitHub Desktop.
Save williamgilpin/014caa2f613c94633a49 to your computer and use it in GitHub Desktop.
Add parameter values to a figure pdf
import PyPDF2 as pdf
def fig_annotate(filename, params):
'''
Given a figure and a dictionary of values (like simulation parameter values)
associated with that figure, write in the "Keyword" metadata of the PDF the parameter list.
When conducting numerical simulations involving large numbers of parameters, it
can be difficult and confusing to keep track of which parameters are associated with which
figures. This function provides an additional check by allowing paramter values to be written directly
into the PDF file
William Gilpin, 2015
'''
inpdf = pdf.PdfFileReader(filename)
mypage = inpdf.getPage(0)
outpdf = pdf.PdfFileWriter()
outpdf.addPage(mypage)
metadata_string = ''
for item in params:
metadata_string += str(item) + '='+str(params[item])
metadata_string += ' '
outpdf.addMetadata({'/Keywords': metadata_string})
outputStream = open('intest.pdf', 'wb')
outpdf.write(outputStream)
outputStream.close()
# pvals = {'b':78, 'mg':0.03e-7}
# fig_annotate('intest.pdf', pvals)
def fig_get_annotations():
'''
Given a figure with annotations written into the "keyword" field
using the format of fig_annotate.py, read those parameter values
and return a parameter dictionary
NOT YET IMPLEMENTED
'''
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment