@@ -1882,6 +1882,63 @@ def new_figure_manager(num, *args, **kwargs):
18821882 manager = FigureManagerPdf (canvas , num )
18831883 return manager
18841884
1885+ class PdfPages (object ):
1886+ """
1887+ A multi-page PDF file.
1888+
1889+ Use like this:
1890+
1891+ # Initialize:
1892+ pdf_pages = PdfPages('foo.pdf')
1893+
1894+ # As many times as you like, create a figure fig, then either:
1895+ fig.savefig(pdf_pages, format='pdf') # note the format argument!
1896+ # or:
1897+ pdf_pages.savefig(fig)
1898+
1899+ # Once you are done, remember to close the object:
1900+ pdf_pages.close()
1901+
1902+ (In reality PdfPages is a thin wrapper around PdfFile, in order to
1903+ avoid confusion when using savefig and forgetting the format
1904+ argument.)
1905+ """
1906+ __slots__ = ('_file' ,)
1907+
1908+ def __init__ (self , filename ):
1909+ """
1910+ Create a new PdfPages object that will be written to the file
1911+ named *filename*. The file is opened at once and any older
1912+ file with the same name is overwritten.
1913+ """
1914+ self ._file = PdfFile (filename )
1915+
1916+ def close (self ):
1917+ """
1918+ Finalize this object, making the underlying file a complete
1919+ PDF file.
1920+ """
1921+ self ._file .close ()
1922+ self ._file = None
1923+
1924+ def savefig (self , figure = None , ** kwargs ):
1925+ """
1926+ Save the Figure instance *figure* to this file as a new page.
1927+ If *figure* is a number, the figure instance is looked up by
1928+ number, and if *figure* is None, the active figure is saved.
1929+ Any other keyword arguments are passed to Figure.savefig.
1930+ """
1931+ if isinstance (figure , Figure ):
1932+ figure .savefig (self , format = 'pdf' , ** kwargs )
1933+ else :
1934+ if figure is None :
1935+ figureManager = Gcf .get_active ()
1936+ else :
1937+ figureManager = Gcf .get_fig_manager (figure )
1938+ if figureManager is None :
1939+ raise ValueError , "No such figure: " + `figure`
1940+ else :
1941+ figureManager .canvas .figure .savefig (self , format = 'pdf' )
18851942
18861943class FigureCanvasPdf (FigureCanvasBase ):
18871944 """
@@ -1905,19 +1962,19 @@ def print_pdf(self, filename, **kwargs):
19051962 image_dpi = kwargs .get ('dpi' , 72 ) # dpi to use for images
19061963 self .figure .set_dpi (72 ) # there are 72 pdf points to an inch
19071964 width , height = self .figure .get_size_inches ()
1908- if isinstance (filename , PdfFile ):
1909- file = filename
1965+ if isinstance (filename , PdfPages ):
1966+ file = filename . _file
19101967 else :
19111968 file = PdfFile (filename )
19121969 file .newPage (width , height )
19131970 renderer = MixedModeRenderer (
19141971 width , height , 72 , RendererPdf (file , image_dpi ))
19151972 self .figure .draw (renderer )
19161973 renderer .finalize ()
1917- if file != filename : # we opened the file
1918- file .close ()
1919- else : # multipage file; just finish off the page
1974+ if isinstance (filename , PdfPages ): # finish off this page
19201975 file .endStream ()
1976+ else : # we opened the file above; now finish it off
1977+ file .close ()
19211978
19221979class FigureManagerPdf (FigureManagerBase ):
19231980 pass
0 commit comments