Q:

Save file after converting from pdf to txt

I have converted some pdf files to read in txt format. However, how do I save it after converting? I was trying to use file.write('file1') option, but it seems not to be working. 
file1 = pdf_to_txt("important_file_1.pdf")
file2 = pdf_to_txt("important_file_2.pdf")

Thank you for the help.

A:

You need to open a new file with write mode:
file1 = pdf_to_txt("important_file_1.pdf")
f = open('pdf_to_text.txt', 'w')
f.write(file1)
f.close()

To make it reusable do something like this:
import time
def save_pdf_to_text(file_to_save, filename=None):
    if not filename:
        timestr = time.strftime("%Y-%m-%d-%H-%M-%S")
        filename = '{}.txt'.format(timestr)
    with open(filename, 'w') as f:
        f.write(file_to_save)

Usage:
file1 = pdf_to_txt("important_file_1.pdf")
save_pdf_to_text(file1)

it will create a file with timestamp. Or pass filename as second argument.

