Ok I add the attachment now, I will be more concise.
I have a script that extracts all PDF in base64 from database and convert them, 
then I cancel someone and one other script (Python) rename them. But you can 
see it also from the GUI, I send the attachment video and I will paste below 
the script that renames, I can't insert the script which extracts from the 
production database for obvious reason.
How you can see in the video, I delete a file, rename one other and It restores 
the old preview.
Remember that everything has an incremental count at the end, and the same 
name, like example_1, example_2, example_3 ....

import os
import re
import sys

BASE_DIR: str = os.path.dirname(os.path.abspath(__file__))
FOLDER_PDF: str = os.path.join(BASE_DIR, "test")


def rename_pdfs_in_folder(folder: str, folder_path: str) -> int:
    pattern: re.Pattern[str] = re.compile(rf"^{re.escape(folder)}_(\d+)\.pdf$")
    pdf_files: list[tuple[int, str]] = []

    for f in os.listdir(folder_path):
        m: re.Match[str] | None = pattern.match(f)
        if m:
            pdf_files.append((int(m.group(1)), f))

    if not pdf_files:
        return 0

    pdf_files.sort(key=lambda x: x[0])

    rename_count = 0
    for new_idx, (_, old_name) in enumerate(pdf_files, start=1):
        new_name: str = f"{folder}_{new_idx}.pdf"
        old_path: str = os.path.join(folder_path, old_name)
        new_path: str = os.path.join(folder_path, new_name)
        if old_name != new_name:
            print(f"  Rinomino: {old_name} -> {new_name}")
            os.rename(old_path, new_path)
            rename_count += 1

    return rename_count


def main() -> None:
    if not os.path.isdir(FOLDER_PDF):
        print(f"Folder {FOLDER_PDF} doesn't found")
        sys.exit(1)

    supplier_dirs: list[str] = sorted(
        d for d in os.listdir(FOLDER_PDF)
        if os.path.isdir(os.path.join(FOLDER_PDF, d))
    )

    if not supplier_dirs:
        print("No subfolders found")
        sys.exit(0)

    total = sum(
        rename_pdfs_in_folder(folder, os.path.join(FOLDER_PDF, folder))
        for folder in supplier_dirs
    )
    print(f"\nRenamed {total} file.")


if __name__ == "__main__":
    main()

** Attachment added: "Screencast From 2026-06-08 21-28-49.mp4"
   
https://bugs.launchpad.net/ubuntu/+source/nautilus/+bug/2155631/+attachment/5976225/+files/Screencast%20From%202026-06-08%2021-28-49.mp4

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2155631

Title:
  Error of preview of PdF

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/nautilus/+bug/2155631/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to