Python 如何从Jupyter笔记本将PdfileWriter()写入硬盘
下面的程序适用于我需要它做的事情,那就是在一个包含多个页面的PDF中阅读,将其拆分为单独的页面,并根据我的贷款编号列表重命名它们。然而,目前新的PDF文件位于我的Jupyter笔记本文件目录中。我需要他们去我的C驱动器,并最终到一个sharepoint网站。有人能帮我吗?下面是函数。我应该将输出路径放在哪里Python 如何从Jupyter笔记本将PdfileWriter()写入硬盘,python,pdf,jupyter-notebook,Python,Pdf,Jupyter Notebook,下面的程序适用于我需要它做的事情,那就是在一个包含多个页面的PDF中阅读,将其拆分为单独的页面,并根据我的贷款编号列表重命名它们。然而,目前新的PDF文件位于我的Jupyter笔记本文件目录中。我需要他们去我的C驱动器,并最终到一个sharepoint网站。有人能帮我吗?下面是函数。我应该将输出路径放在哪里 output_path = 'C:/output' import os from PyPDF2 import PdfFileReader, PdfFileWriter #counter =
output_path = 'C:/output'
import os
from PyPDF2 import PdfFileReader, PdfFileWriter
#counter = 0
def pdf_splitter(path):
fname = os.path.splitext(os.path.basename(path))[0]#grab the name of the input file, minus the extension.
counter = 0
pdf = PdfFileReader(path) #open the PDF up and create a reader object.
for page in range(pdf.getNumPages()): #loop over all the pages using the reader object’s getNumPages method.
pdf_writer = PdfFileWriter() #create an instance of PdfFileWriter.
pdf_writer.addPage(pdf.getPage(page)) #add a page to our writer object using its addPage method which accespts a page
#object, so we call the reader object's getPage method. Now we have added one page to our writer object.
output_filename = '{}_Loan Number_{}.pdf'.format( #create a unique file name which we do by using the original file name plus
#the word "loan number" plus iterate over loanNumber number list. Page will increment since it's the control variable.
fname, loanNumber[page])
with open(output_filename, 'wb') as out: #open the new file name in write-binary mode and use the PDF writer object’s write
#method to write the object’s contents to disk.
pdf_writer.write(out)
print('Created: {}'.format(output_filename))
counter += 1
print counter
if __name__ == '__main__':
path = 'MSPimage.pdf'
pdf_splitter(path)