{"id":3889,"date":"2023-11-27T22:54:04","date_gmt":"2023-11-27T14:54:04","guid":{"rendered":"http:\/\/cnliutz.wicp.vip\/?p=3889"},"modified":"2023-11-27T22:54:06","modified_gmt":"2023-11-27T14:54:06","slug":"python%e8%af%bb%e5%8f%96pdf%e6%96%87%e4%bb%b6%e4%b8%ad%e7%9a%84%e6%8c%87%e5%ae%9a%e9%a1%b5%e7%a0%81%e7%9a%84%e8%8c%83%e5%9b%b4%e5%b9%b6%e7%9a%84%e5%ad%98%e5%82%a8%e5%88%b0%e6%8c%87%e5%ae%9a%e7%9a%84","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=3889","title":{"rendered":"python\u8bfb\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u9875\u7801\u7684\u8303\u56f4\u5e76\u7684\u5b58\u50a8\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u540d"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\r\n\"\"\"\r\nCreated on Mon Nov 27 21:36:12 2023\r\n\r\n@author: cnliu\npip install pypdf2\r\n\"\"\"\r\nfrom PyPDF2 import PdfWriter, PdfReader\r\nimport os\r\n \r\n#pathToPDF = input('something like \/home\/pedro\/Latin\/ ... ')\r\npathToPDF = \"d:\/\u4e66\/\"\r\npath2Extracts = 'd:\/'\r\n# get the names of the files available to extract from\r\nfiles = os.listdir(pathToPDF)\r\n# show the files in a loop so you can choose 1\r\n# I haven't done that here\r\n# choose a PDF from a list of PDFs from  as bookname\r\n#bookTitle = bookname.replace('.pdf', '')\r\nbookname = \"AHaSuanFa.pdf\"\r\n# read the pdf\r\npdf = PdfReader(pathToPDF + bookname)\r\n#pages = pdf.getNumPages() (deprecated)\r\npages = len(pdf.pages)\r\nprint('This pdf has ' + str(pages) + ' pages')\r\nprint('What pages do you want to get?')\r\nstartnum = input('what is the starting page number?  ')\r\nprint('If your last page is page 76, enter 76 for the end number')\r\nendnum = input('what is the last page number?  ')\r\nstart = int(startnum) - 1\r\nend = int(endnum)\r\n# only need to open pdfWriter 1 time\r\npdf_writer = PdfWriter()\r\nfor page in range(start, end):\r\n        pdf_writer.add_page(pdf.pages&#91;page])\r\n         \r\nprint('Enter the savename for this pdf, like CE3U8')\r\nsavename = input('Enter the name to save this pdf under, like CE3U8 No need to add .pdf ... ')\r\noutput_filename = savename + '.pdf'\r\n \r\nwith open(path2Extracts + output_filename, 'wb') as out:\r\n        pdf_writer.write(out)\r\nprint(f'Created: {output_filename} and saved in', path2Extracts)\r\nprint('All done!')<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-3889","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/3889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3889"}],"version-history":[{"count":1,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/3889\/revisions"}],"predecessor-version":[{"id":3890,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/3889\/revisions\/3890"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3889"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}