-
where am I doing wrong? import fitz
import tkinter as tk
from tkinter import filedialog
import os
import shutil
root = tk.Tk()
root.withdraw()
main_path = filedialog.askdirectory(title="Pdf'lerin bulundugu klasoru seciniz.")
dst_path = filedialog.askdirectory(title="Taşınacak klasör")
pdfs = []
for doc in os.listdir(main_path):
if doc.endswith(".pdf"):
pdfs.append(os.path.join(main_path, doc))
for p in pdfs:
doc = fitz.Document(p)
new_path = os.path.join(dst_path, os.path.basename(p))
d_pages = []
for i in range(len(doc)):
text = doc[i].get_text()
print(i)
if not "Ruhsat No:408-MRK" in text:
d_pages.append(i)
print(d_pages)
for d in d_pages:
doc.delete_page(d+1)
doc.save(new_path)
doc.close()
output: 0
1
2
3
4
[0, 1, 2, 3]
Traceback (most recent call last):
File "c:\Users\user\Desktop\istakademisonuc\alab dısışndakileri sil.py", line 31, in <module>
doc.delete_page(d+1)
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\fitz\fitz.py", line 5404, in delete_page
raise ValueError("bad page number(s)")
ValueError: bad page number(s) |
Beta Was this translation helpful? Give feedback.
Answered by
JorjMcKie
Aug 12, 2023
Replies: 1 comment 2 replies
-
When you delete a page, then pages coming after it are renumbered of course. The number is not a fixed property of a page, but like an index in a list. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
kahvecci
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you delete a page, then pages coming after it are renumbered of course. The number is not a fixed property of a page, but like an index in a list.
After deleting 0 from the Python list
[0, 1, 2, 3, 4, 5]
, 1 will be found at index 0 and you cannot delete it using index 1.In order to delete pages by numbers contained in a list, make sure to sort that list and then start with deleting in reversed order: larger page numbers first.