Hi, I am trying to get a script written up to rename PDF and word documents in a certain folder by using the text in the documents.
I need them named: LASTNAME.Firstname CLIENT
the information would come from a timesheet so people would be entering that information into a table
- would this be possible ?
What I have so far is this, but I don't know how to get the information from the document table as each one would be different?
I am open to any suggestions on how this could work
I need them named: LASTNAME.Firstname CLIENT
the information would come from a timesheet so people would be entering that information into a table
What I have so far is this, but I don't know how to get the information from the document table as each one would be different?
I am open to any suggestions on how this could work
import os from docx import Document import PyPDF2 def extract_text_from_docx(docx_path): doc = Document(docx_path) text = "" for paragraph in doc.paragraphs: text += paragraph.text return text def extract_text_from_pdf(pdf_path): text = "" with open(pdf_path, 'rb') as file: reader = PyPDF2.PdfFileReader(file) for page_num in range(reader.numPages): text += reader.getPage(page_num).extractText() return text def main(): directory = "path/to/your/documents" for filename in os.listdir(directory): if filename.endswith(".docx"): full_path = os.path.join(directory, filename) new_name = extract_text_from_docx(full_path) elif filename.endswith(".pdf"): full_path = os.path.join(directory, filename) new_name = extract_text_from_pdf(full_path) else: continue # Rename the file os.rename(full_path, os.path.join(directory, new_name + os.path.splitext(filename)[1])) if __name__ == "__main__": main()
Gribouillis write Mar-19-2024, 09:06 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.