-
-
Notifications
You must be signed in to change notification settings - Fork 342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Rename PDF Sheets not working #2273
Comments
Hi @countdookuw, sorry for being so late in replying! Do you by any chance still have the list of PDF file names? The script expects that the file is named like The error you see is because one of the file in the chosen directory has "Sheet" in it, so it passes the first test Line 33 in f9fa0d2
but it doesn't contain "Sheet - " (with space, hyphen and space after the word) as checked with the regular expression here: Line 19 in f9fa0d2
This bug can be resolved in 2 ways: checking the presence of the entire "Sheet - " text from the start, or use a better code for the |
Hello everyone ! i have tried différent méthode to solve this issus and test run time, it's not so much but i thing it's faster without regex : if it's ok i will also add a subfolder option with a form
|
I experimented with both functions in regex and normal string. Here is what I came up with in regex : def rename_pdf_re(old_name):
try:
# Step 1: Delete everything before and including "Sheet -", with space management
new_name = re.sub(r"^.*?Sheet\s*-\s*", "", old_name)
# Step 2: Make the part after the last hyphen before the period capitalized
new_name = re.sub(r"\s*-\s*(.*)\.", lambda m: "-{}.".format(m.group(1).upper()), new_name)
# Step 3: Normalize all other hyphens surrounded by spaces to single hyphens with no spaces (Optional)
new_name = re.sub(r"\s*-\s*", "-", new_name)
except:
return False
# Test new_name validity
if new_name == old_name or new_name == "":
return False
else:
return new_name Here is what I came up with normal string (does include step 3, which replaces all spaces and hyphens with just a hyphen) : def rename_pdf_str(old_name):
#The non regex fonction may be litle bite faster It is more complex to manage the different cases with or without spaces
try:
index_sheet = old_name.find("Sheet - ")
if index_sheet != -1:
new_name = old_name[index_sheet + len("Sheet - "):]
new_name = new_name[:new_name.find("-")+1] + " " + new_name[new_name.find("-") + 1:].strip().upper()
else:
return False
except:
return False
if new_name == old_name or new_name == "":
return False
else:
return new_name As I mentioned, I added the option to also handle subfolders. if not forms.alert("Do you want to include subfolders?", yes=True, no=True):
#No sub Folder treatment proced for basefolder
sheetcount = sheetcount + process_folder(basefolder)
else:
# Search for pdf files in base folder and sub folder, add folder who have any in the list
folders_to_process = []
for root, dirs, files in os.walk(basefolder):
if any(f.lower().endswith(".pdf") for f in files):
folders_to_process.append(root)
if not folders_to_process:
forms.alert("No .pdf file found in these folder")
#list not empty proced for each folder
else:
for folder in folders_to_process:
sheetcount = sheetcount + process_folder(folder)
# let user know how many sheets have been renames
forms.alert("{0} FILES RENAMED.".format(sheetcount)) I don't know yet how to push or propose a modification in the project code !? Happy New Year and enjoy your holidays if you have some ! 🎉✨🎄 Thanks, everyone, for the effort you put in ! |
Hi @Malouche-git , thank you for the contribution! Indeed the regex way is slower, but it can be optimized by compiling the patterns with
prefix_stripper = re.compile(r"^.*?Sheet\s*-\s*")
capitalizer = re.compile(r"\s*-\s*(.*)\.")
normalizer = re.compile("\s*-\s*")
def capitalizer_repl(match):
return "-{}.".format(m.group(1).upper())
def rename_pdf(old_name):
new_name = prefix_stripper.sub("", old_name)
new_name = capitalizer.sub(capitalizer_repl, new_name)
return normalizer.sub("-", new_name) I'm against returning def rename_pdf(old_name):
new_name = prefix_stripper.sub("", old_name)
new_name = capitalizer.sub(capitalizer_repl, new_name)
new_name = normalizer.sub("-", new_name)
if not new_name:
raise ValueError("Renaming results in an empty string.")
return new_name Also, the file operations can be simplified with from pathlib import Path
# ...
dir_pattern = "**/" if forms.alert("Do you want to include subfolders?", yes=True, no=True) else ""
for pdf_file in Path(basefolder).glob("{}*.pdf".format(dir_pattern)):
try:
new_name = rename_pdf(pdf_file.stem)
except (ValueError, re.error):
continue
new_path = pdf_file.with_name("{}.pdf".format(new_name))
pdf_file.rename(new_path) Finally, to send the proposed changes you have to:
You can read the official github documentation, if you want to do the "full-contributor" way and work on a local copy synced to your repository |
✈ Pre-Flight checks
🐞 Describe the bug
Rename PDF Sheets does not work
⌨ Error/Debug Message
♻️ To Reproduce
⏲️ Expected behavior
Rename PDF tool to work
🖥️ Hardware and Software Setup (please complete the following information)
Additional context
Running Windows 11 Pro on Lenovo Thinkpad , Revit 2024
The text was updated successfully, but these errors were encountered: