{"id":307,"date":"2019-09-03T08:38:23","date_gmt":"2019-09-03T00:38:23","guid":{"rendered":"http:\/\/www.lnzdl.com\/blog\/?p=307"},"modified":"2019-09-03T08:45:35","modified_gmt":"2019-09-03T00:45:35","slug":"%e5%88%a9%e7%94%a8pypdf2%e6%a8%a1%e5%9d%97%e5%80%92%e5%ba%8fpdf%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/www.lnzdl.com\/blog\/%e5%88%a9%e7%94%a8pypdf2%e6%a8%a1%e5%9d%97%e5%80%92%e5%ba%8fpdf%e6%96%87%e4%bb%b6\/","title":{"rendered":"\u5229\u7528PyPDF2\u6a21\u5757\u5012\u5e8fPDF\u6587\u4ef6"},"content":{"rendered":"<p>\u6709\u65f6\u4f7f\u7528\u626b\u63cf\u4eea\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u626b\u63cf\u987a\u5e8f\u662f\u53cd\u7684\uff0c\u7f51\u4e0a\u627e\u4e86\u4e00\u5708\u6ca1\u6709\u53d1\u73b0\u9002\u7528\u7684\u7a0b\u5e8f\uff0c\u4e00\u9875\u4e00\u9875\u79fb\u52a8\u53c8\u5f88\u9ebb\u70e6\u3002\u4e8e\u662f\u5c31\u60f3\u51fa\u4e86\u8fd9\u4e2a\u5c0f\u529f\u80fd\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u9700\u5b89\u88c5PyPdf2<\/p>\n<p>\u4f7f\u7528\u65f6\uff0c\u7528\u7684\u76f8\u5bf9\u4f4d\u7f6e3.pdf\uff0c\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u66f4\u6539\u3002\u529f\u80fd\u5c31\u662f\u8fd9\u4e2a\u529f\u80fd\uff0c\u5176\u5b9e\u53ef\u4ee5\u914d\u5408UI\u505a\u6210\u4e00\u4e2aEXE\uff0c\u4f46\u592a\u5c0f\u4f17\u4e86\uff0c\u4f9b\u5927\u5bb6\u5b66\u4e60\u5c31\u597d\u4e86\u3002<\/p>\n<blockquote><p># -*- coding:utf-8*-<br \/>\n# \u250d\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513<br \/>\n# \u2503\u5229\u7528PyPDF2\u6a21\u5757\u5012\u5e8fPDF\u6587\u4ef6 \u2503<br \/>\n# \u2503\u7f16\u5236\uff1alnzdl www.lnzdl.com \u2503<br \/>\n# \u2503 2019.9.3\u2503<br \/>\n# \u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251b<\/p>\n<p>import os<br \/>\nfrom PyPDF2 import PdfFileReader, PdfFileWriter<br \/>\nimport time<\/p>\n<p># \u5408\u5e76\u540c\u4e00\u76ee\u5f55\u4e0b\u7684\u6240\u6709PDF\u6587\u4ef6<br \/>\ndef ReversePDF(file, outfile):<\/p>\n<p>output = PdfFileWriter()<br \/>\noutputPages = 0<\/p>\n<p>if file:<br \/>\n# \u8bfb\u53d6\u6e90PDF\u6587\u4ef6<br \/>\ninput = PdfFileReader(open(file, &#8220;rb&#8221;))<\/p>\n<p># \u83b7\u5f97\u6e90PDF\u6587\u4ef6\u4e2d\u9875\u9762\u603b\u6570<br \/>\npageCount = input.getNumPages()<br \/>\nprint(&#8220;\u6587\u4ef6\u9875\u6570\uff1a%d&#8221;%pageCount)<br \/>\noutputPages_num=0<\/p>\n<p># \u5206\u522b\u5c06page\u6dfb\u52a0\u5230\u8f93\u51faoutput\u4e2d<br \/>\nfor iPage in range(pageCount-1,-1,-1):<br \/>\noutput.addPage(input.getPage(iPage))<br \/>\noutputPages_num +=1<br \/>\nprint(&#8220;\u603b\u9875\u6570:%d&#8221;%outputPages_num)<\/p>\n<p># \u5199\u5165\u5230\u76ee\u6807PDF\u6587\u4ef6<br \/>\noutputStream = open(outfile, &#8220;wb&#8221;)<br \/>\noutput.write(outputStream)<br \/>\noutputStream.close()<br \/>\nprint(&#8220;PDF\u6587\u4ef6\u5408\u5e76\u5b8c\u6210\uff01&#8221;)<\/p>\n<p>else:<br \/>\nprint(&#8220;\u6ca1\u6709\u53ef\u4ee5\u5408\u5e76\u7684PDF\u6587\u4ef6\uff01&#8221;)<\/p>\n<p># \u4e3b\u51fd\u6570<br \/>\ndef main():<br \/>\ntime1 = time.time()<br \/>\nfile=&#8221;3.pdf&#8221; # \u9700\u8981\u5012\u5e8f\u7684PDF\u6587\u4ef6<br \/>\noutfile = &#8220;2.pdf&#8221; # \u8f93\u51fa\u7684PDF\u6587\u4ef6\u540d\u79f0<br \/>\nReversePDF(file, outfile)<br \/>\ntime2 = time.time()<br \/>\nprint(&#8216;\u603b\u5171\u8017\u65f6\uff1a%s s.&#8217; %(time2 &#8211; time1))<\/p>\n<p>main()<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u4f7f\u7528\u626b\u63cf\u4eea\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u626b\u63cf\u987a\u5e8f\u662f\u53cd\u7684\uff0c\u7f51\u4e0a\u627e\u4e86\u4e00\u5708\u6ca1\u6709\u53d1\u73b0\u9002\u7528\u7684\u7a0b\u5e8f\uff0c\u4e00\u9875\u4e00\u9875\u79fb\u52a8\u53c8\u5f88\u9ebb\u70e6\u3002\u4e8e\u662f\u5c31\u60f3\u51fa\u4e86\u8fd9\u4e2a\u5c0f\u529f\u80fd\u7684\u5b9e\u73b0\u3002 \u9700\u5b89\u88c5PyPdf2 \u4f7f\u7528\u65f6\uff0c\u7528\u7684\u76f8\u5bf9\u4f4d\u7f6e3.pdf\uff0c\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u66f4\u6539\u3002\u529f\u80fd\u5c31\u662f\u8fd9\u4e2a\u529f\u80fd\uff0c\u5176\u5b9e\u53ef\u4ee5\u914d\u5408UI\u505a\u6210\u4e00\u4e2aEXE\uff0c\u4f46\u592a\u5c0f\u4f17\u4e86\uff0c\u4f9b\u5927\u5bb6\u5b66\u4e60\u5c31\u597d\u4e86\u3002 # -*- coding:utf-8*- # \u250d\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513 # \u2503\u5229\u7528PyPDF2\u6a21\u5757\u5012\u5e8f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14],"_links":{"self":[{"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/posts\/307"}],"collection":[{"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/comments?post=307"}],"version-history":[{"count":2,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":312,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/posts\/307\/revisions\/312"}],"wp:attachment":[{"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}