Search code examples
pythonpypdf

How to change the directory where PDFs are saved?


I am developing an application to split PDFs and mining the internet I managed to do it, however, I would like to change the folder where the PDFs are saved. Can you help me?

Here is the code below:

from PyPDF2 import PdfFileWriter, PdfFileReader
from tkinter.filedialog import askopenfilename
from tkinter import messagebox


inputpdf = PdfFileReader(askopenfilename(), "rb")
print(inputpdf)

for i in range(inputpdf.numPages):
    output = PdfFileWriter()
    output.addPage(inputpdf.getPage(i))
    with open("document-page%s.pdf" % i, "wb") as outputStream:
        output.write(outputStream)
print("PDF Dividido com sucesso.")
messagebox.showinfo("System Message", "PDF successfully divided.")

Solution

  • When you do open("document-page.pdf") you can insert a pathname where document-page.pdf is. For example ~/Documents/Some_random_folder/new_file.pdf