Python Forum

Full Version: Scanning files to rename other files in the same folder.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Just to make sure I understand: You want to convert a bunch of tif files into pdf files with the same name? If that is the case, you shouldn't put them all into one pdf and then try to split up the pdf. You should just write them out to individual pdfs as you convert them.

os.chdir(r"F:\Tif files")
for tif_index, tif_file in enumerate(glob.glob("*.tif"), start = 1):
    # Converts all .tif files into a single PDF file.
    with open("Invoice-page{}.pdf".format(tif_index), "wb") as f:
        f.write(img2pdf.convert([tif_file]))
Also, file is a built-in function is Python. Using it for a variable name can mess up other code that is expecting it to be the built-in. I also used the format method of strings, which is similar to but more powerful than the old % formatting. If you have Python 3.6+ you can use the even newer f-string syntax.
Thank you ichabod. This helped me clean up the code a lot. Only problem is, and this is where my original code was kind of helpful was I still need to get the files to be renamed to their .tif original file names. Essentially I want to have them go from 123456.tif to 123456.pdf. Got any suggestions?
Instead of "Invoice-page{}.pdf".format(tif_index), use "{}.pdf".format(tif_file[:-4]).
Thank you again Ichabod. When I have some spare time today I'll try it out. I'm curious now, is this something that comes with python or is this something related to a module? I'd like to read more about it to get a better understanding for future reference.
(Feb-28-2019, 02:17 PM)msd1391 Wrote: [ -> ]I'm curious now, is this something that comes with python or is this something related to a module?

Which? The format method is standard Python. Documentation is here and here. If you have Python 3.6+ you can use the even newer f-string syntax.
Thank you again Ichabod and my apologies for the late reply. Been busy at work with a few things. I'll take a look at these when I have some spare time this weekend.