微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从Jupyter Notebook将PdfFileWriter写入我的硬盘

如何解决如何从Jupyter Notebook将PdfFileWriter写入我的硬盘

下面的程序适用于我需要做的事情,即读取多页的PDF,将其分成多个页面,然后根据我的贷款编号列表重命名。但是,当前新的pdf位于我的Jupyter笔记本文件目录中。我需要他们去我的C盘,最后去一个共享点站点。有人能帮我吗?下面是功能。我应该在哪里放置我的output_path?

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。