I must rename a number of PDF files with the serial numbers of laptops every day as part of my routine. I need somekind of a script/software that opens a PDF file, copies a certain line (service tag) from it, then closes and renames the PDF with the copied serial number from it.
I tried to use the PyPDF2 package in Python, but my code is not working.
from PyPDF2 import PdfReader
reader = PdfReader("SNPDF.pdf")
page = reader.pages[0]
number_of_pages = len(reader.pages)
text = page.extract_text()
for x in text:
if x.startswith("SN"):
new_name = x.replace("SN", '')
print(new_name)
It worked with .txt file, but I have no clue how to run it with PDF.
import os
serial = open('text.txt')
for line in serial:
if line.startswith("SN"):
new_line = line.replace('SN','')
serial.close()
os.rename('text.txt', new_line)
The following code should extract the service tag from the first page of the PDF file and rename the file with the extracted service tag.