#! /usr/bin/env Python3

import PyPDF2
import os
"""
#print(os.getcwd())

os.chdir('/Users/ymccarter/PycharmProjects/Mihon/working_mihon/Renshu_folder/automationPython/FileDirectory')

pdfFile=open('/Users/ymccarter/Downloads/meetingminutes1.pdf','rb')

reader=PyPDF2.PdfFileReader(pdfFile)
reader.numPages
page=reader.getPage(0)
print(page.extractText())


for pageNum in range(reader.numPages): #shows contents of PDF
    print(reader.getPage(pageNum).extractText())


#SAY WE WANT TO COMBINE MULTIPE PDF FILE. IN THIS SCENARIO MEETINGMINUTE 1 AND 2:

os.chdir('/Users/ymccarter/PycharmProjects/Mihon/working_mihon/Renshu_folder/automationPython/FileDirectory')

pdfFile1=open('/Users/ymccarter/Downloads/meetingminutes1.pdf','rb')
pdfFile2=open('/Users/ymccarter/Downloads/meetingminutes2.pdf','rb')
reader1=PyPDF2.PdfFileReader(pdfFile1)
reader2=PyPDF2.PdfFileReader(pdfFile2)

print(reader1.numPages) # numPages in PyPDF2 will count the number of page
print(reader2.numPages)

# and you can use 'reader.getPage('page#') ' to take the object of specified page
#page.extractText() will show the text format of content

page=reader1.getPage(0)
print(page.extractText())


#you can create a loop to extract page contents too:

for pageNum in range(reader1.numPages):
    print(reader1.getPage(pageNum).extractText())
"""

#writer function

#create the loop and add each page to add each page of two PDFs

pdfFile1=open('/Users/ymccarter/Downloads/meetingminutes1.pdf','rb')
pdfFile2=open('/Users/ymccarter/Downloads/meetingminutes2.pdf','rb')
reader1=PyPDF2.PdfFileReader(pdfFile1)
reader2=PyPDF2.PdfFileReader(pdfFile2)

writer = PyPDF2.PdfFileWriter() #creating written object

for pageNum in range(reader1.numPages):
    page = reader1.getPage(pageNum)
    writer.addPage(page) #adding the file1 to written object

for pageNum in range(reader2.numPages):
    page = reader2.getPage(pageNum)
    writer.addPage(page) #adding the file2 on the top of file1 to the written object.

outputfile=open('combinedminutes.pdf','wb')
writer.write(outputfile)


pdfFile2.close()
pdfFile1.close()
outputfile.close()


pdfFile3=open('combinedminutes.pdf','rb')
reader3=PyPDF2.PdfFileReader(pdfFile3)

print(reader3.numPages)
for pageNum in range(reader3.numPages): #shows contents of PDF
    print(reader3.getPage(pageNum).extractText())