Feb-26-2019, 03:07 PM
I'm fairly new to programming and python in general. At my office, my manager allowed me to write some code to help our department convert invoices that come out of the system as .tif files to PDF's. I managed to get the code to work but now I've hit a wall in getting the PDF files to be renamed to their tif origins. Here is the code that I have so far:
import os, sys, glob, img2pdf # Scans directory folder for any .tif files. os.chdir(r"F:\Tif files") for file in glob.glob("*.tif"): print(file) # Converts all .tif files into a single PDF file. with open("Invoice.pdf", "wb") as f: f.write(img2pdf.convert([i for i in os.listdir(r'F:\Tif files') if i.endswith(".tif")])) from PyPDF2 import PdfFileWriter, PdfFileReader # Breaks down the PDF file into individual pages in directory. inputpdf = PdfFileReader(open("Invoice.pdf", "rb")) for i in range(inputpdf.numPages): output = PdfFileWriter() output.addPage(inputpdf.getPage(i)) with open("Invoice-page%s.pdf" % i, "wb") as outputStream: output.write(outputStream)I've researched on google and stack overflow and most of the information just involves physically renaming a file similar to how I have it in my code. At this point I'm at a loss and maybe I'm just not searching properly. If anyone has any suggestions, whether it be to clean this up so that I can get the results I want or knows of a module I can look into, I'd greatly appreciate the help.