Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc94ddb

Browse files
committed
Merge pull request matplotlib#2440 from megies/pdfpages_pagecount
Pdfpages pagecount convenience getter method
2 parents 9666324 + cccc22a commit fc94ddb

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,6 +2310,12 @@ def savefig(self, figure=None, **kwargs):
23102310
else:
23112311
figureManager.canvas.figure.savefig(self, format='pdf', **kwargs)
23122312

2313+
def get_pagecount(self):
2314+
"""
2315+
Returns the current number of pages in the multipage pdf file.
2316+
"""
2317+
return len(self._file.pageList)
2318+
23132319
class FigureCanvasPdf(FigureCanvasBase):
23142320
"""
23152321
The canvas the figure renders into. Calls the draw and print fig

lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,23 @@ def test_type42():
3737
ax = fig.add_subplot(111)
3838
ax.plot([1,2,3])
3939
fig.savefig(io.BytesIO())
40+
41+
42+
@cleanup
43+
def test_multipage_pagecount():
44+
from matplotlib.backends.backend_pdf import PdfPages
45+
from io import BytesIO
46+
with PdfPages(BytesIO()) as pdf:
47+
assert pdf.get_pagecount() == 0
48+
fig = plt.figure()
49+
ax = fig.add_subplot(111)
50+
ax.plot([1, 2, 3])
51+
fig.savefig(pdf, format="pdf")
52+
assert pdf.get_pagecount() == 1
53+
pdf.savefig()
54+
assert pdf.get_pagecount() == 2
55+
56+
57+
if __name__ == '__main__':
58+
import nose
59+
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)