Python Forum
Scanning files to rename other files in the same folder.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scanning files to rename other files in the same folder.
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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?
Reply
#4
Instead of "Invoice-page{}.pdf".format(tif_index), use "{}.pdf".format(tif_file[:-4]).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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.
Reply
#6
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 334 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
Question Right way to open files with different encodings? Winfried 2 182 Apr-23-2024, 05:50 PM
Last Post: snippsat
  Open files in an existing window instead of new Kostov 2 303 Apr-13-2024, 07:22 AM
Last Post: Kostov
  Using zipfile module - finding folders not files darter1010 2 274 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,059 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  File loop curiously skipping files - FIXED mbk34 10 802 Feb-10-2024, 07:08 AM
Last Post: buran
  Copy Paste excel files based on the first letters of the file name Viento 2 442 Feb-07-2024, 12:24 PM
Last Post: Viento
  Class test : good way to split methods into several files paul18fr 4 486 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 552 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 461 Dec-22-2023, 07:17 AM
Last Post: dchilambo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020