由于OPEND PDF文件而失败的删除目录

发布于 2025-02-13 13:39:51 字数 3281 浏览 0 评论 0原文

大家好,我是新来的,

程序说明:我目前正在开发一个python程序,该程序取消了包含PDF的文件夹。然后,它将PDF的页面分成单页PDF,并将它们分为另一个路径上的文件夹(“/georeferenzieren/0;/georeferenzieren/1; ...),具体取决于页码。 对于代码的这个特定部分,我遵循 tutorial

问题:都可以很好地工作,但是当我尝试删除临时文件夹时,显示错误的文件夹的第一个文件仍在另一个过程中使用。

(ger)

执行期:[WinError 32] Der Prozess Kann Nicht Auf Die Datei Zugreifen,Da Sie von Einem Anderen Anderen prozess prozess verwendet wird:工作\ testumgebung \ temp \ 20220524_0109_v01_auskunft_01_a3_h.pdf'

(en)

执行手术:[WinError 32]该过程无法访问该文件,因为另一个过程正在使用:'e:\ intern \ programMieren \ python工作\ TSTEMUMGEBUNG \ temp \ temp \ 20220524_0109_V01_AUSK​​UNFT01_AUSK​​UNFT_AUSK​​UNFT_AUSK​​UNFT_AUSK​​UNFT_01_A3_A3_A3_A3_A3_PDFFF'

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

执行 强>

  1. 重新启动PC以确保没有程序使用PDF。我认为仍然发生错误,因为PDF是由Python文件操作的。
  2. 添加pdf.close()(请参见下面的代码)
  3. 添加pdf.merger.close(),例如这个家伙建议,因为我不使用合并,所以它不起作用。 (请参见下面的代码)

代码:

#variables from the code befor this passage
run = 0
tempPath = "E:\Intern\Programmieren\Python for Work\Testumgebung\temp"
geoPath = "E:\Intern\Programmieren\Python for Work\Testumgebung\Georeferenzieren"

#splitting up PDFs to singel Page PDFs
#https://www.thepythoncode.com/article/split-pdf-files-in-python
print("###Split up PDFs")

file2pages = {
    0: [0,1], 1: [1,2], 2: [2,3], 3: [3,4], 4: [4,5],
}

for root, directories, file in os.walk(tempPath):
    for file in file:
        run = run + 1
        filePath = os.path.join(tempPath, file)
        pdf = Pdf.open(filePath)
        newPdfFiles = [Pdf.new() for i in file2pages]
        newPdfIndex = 0

        for n, page in enumerate(pdf.pages):
            if n in list(range(*file2pages[newPdfIndex])):
                newPdfFiles[newPdfIndex].pages.append(page)
            else:
                # make a unique filename based on original file name plus the index
                pdfPath = os.path.join(geoPath, str(newPdfIndex), file)
                outputFilename = f"{pdfPath}-{newPdfIndex}.pdf"

                # save the PDF file
                newPdfFiles[newPdfIndex].save(outputFilename)

                newPdfIndex += 1
                # add the `n` page to the `newPdfIndex` file
                newPdfFiles[newPdfIndex].pages.append(page)

        #save last PDF file
        pdfPath = os.path.join(geoPath, str(newPdfIndex), file)
        outputFilename = f"{pdfPath}-{newPdfIndex}.pdf"
        newPdfFiles[newPdfIndex].save(os.path.join(geoPath, "1. Page", outputFilename))
        print(f"Splitting up the {run}. PDF.")

        #pdf.close()
        #Pdf.merger.close()

#deleting temp folder
print(end = "\n")
print("#Removing temp folder...")
shutil.rmtree(tempPath)
time.sleep(2)

问题:我想知道是否有办法在删除文件夹之前关闭已使用的PDF

Hey guys I'm new here,

Program explanation: I'm currently working on a python program that unzips folders which include PDFs to an "temp" folder. It then splits the pages of the PDFs to single page PDFs and sorts them into folders on another path ("/Georeferenzieren/0; /Georeferenzieren/1; ...) depending on the page number.
For this specific part of the code I followed this guys tutorial.

Problem: That all works perfectly fine, but when I try to delete the temp folder, an error is displayed that the first file of the folder is still being used by another process.

(ger)

PermissionError: [WinError 32] Der Prozess kann nicht auf die Datei zugreifen, da sie von einem anderen Prozess verwendet wird: 'e:\Intern\Programmieren\Python for Work\Testumgebung\temp\20220524_0109_V01_Auskunft_01_A3_H.pdf'

(en)

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'e:\Intern\Programmieren\Python for Work\Testumgebung\temp\20220524_0109_V01_Auskunft_01_A3_H.pdf'

What I tried:

  1. Restarting PC to make sure no program uses the PDF. I think the error still occured because the PDF is opend by the python file.
  2. adding pdf.close() (pls see code below)
  3. adding Pdf.merger.close(), like this guy suggested, which didn't work since I'm not using the merger. (pls see code below)

Code:

#variables from the code befor this passage
run = 0
tempPath = "E:\Intern\Programmieren\Python for Work\Testumgebung\temp"
geoPath = "E:\Intern\Programmieren\Python for Work\Testumgebung\Georeferenzieren"

#splitting up PDFs to singel Page PDFs
#https://www.thepythoncode.com/article/split-pdf-files-in-python
print("###Split up PDFs")

file2pages = {
    0: [0,1], 1: [1,2], 2: [2,3], 3: [3,4], 4: [4,5],
}

for root, directories, file in os.walk(tempPath):
    for file in file:
        run = run + 1
        filePath = os.path.join(tempPath, file)
        pdf = Pdf.open(filePath)
        newPdfFiles = [Pdf.new() for i in file2pages]
        newPdfIndex = 0

        for n, page in enumerate(pdf.pages):
            if n in list(range(*file2pages[newPdfIndex])):
                newPdfFiles[newPdfIndex].pages.append(page)
            else:
                # make a unique filename based on original file name plus the index
                pdfPath = os.path.join(geoPath, str(newPdfIndex), file)
                outputFilename = f"{pdfPath}-{newPdfIndex}.pdf"

                # save the PDF file
                newPdfFiles[newPdfIndex].save(outputFilename)

                newPdfIndex += 1
                # add the `n` page to the `newPdfIndex` file
                newPdfFiles[newPdfIndex].pages.append(page)

        #save last PDF file
        pdfPath = os.path.join(geoPath, str(newPdfIndex), file)
        outputFilename = f"{pdfPath}-{newPdfIndex}.pdf"
        newPdfFiles[newPdfIndex].save(os.path.join(geoPath, "1. Page", outputFilename))
        print(f"Splitting up the {run}. PDF.")

        #pdf.close()
        #Pdf.merger.close()

#deleting temp folder
print(end = "\n")
print("#Removing temp folder...")
shutil.rmtree(tempPath)
time.sleep(2)

Question: I would like to know if there is a way to close the used PDF before deleting the folder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-20 13:39:51

这是一个干净的代码,可以做同样的代码。在我的机器上测试。

import os
import shutil
import time
from pathlib import Path

from pikepdf import Pdf

# assuming team and geo are present in your project dir
temp_path = Path(".").joinpath("temp")
geo_path = Path(".").joinpath("geo")

# splitting up PDFs to singel Page PDFs
# https://www.thepythoncode.com/article/split-pdf-files-in-python
print("###Split up PDFs\n")

for root, directories, files in os.walk(temp_path):
    for file in files:
        file_path = temp_path.joinpath(file)
        print(f'processing file {file_path}')

        # HERE
        with Pdf.open(file_path) as pdf:
            file2pages = {i: [i, i+1] for i in range(len(pdf.pages))}
            for pdf_index, pages_range in file2pages.items():
                new_pdf = Pdf.new()
                [new_pdf.pages.append(pdf.pages[i]) for i in range(*pages_range)]
                output_path = geo_path.joinpath(str(pdf_index))
                output_path.mkdir(parents=True, exist_ok=True)
                out_filename = output_path.joinpath(f'{file}-{pdf_index}.pdf')
                print(f'saving {pages_range[0] + 1}-{pages_range[1]} pages as {out_filename}')
                new_pdf.save(out_filename)

# deleting temp folder
print()
print("#Removing temp folder...")
shutil.rmtree(temp_path)
time.sleep(2)

您的代码似乎存在许多问题。因此,重新实现了它。使用观察的使用情况

,如果您仍然遇到错误,则可能需要检查计算机上的文件权限。

编辑:基于将PDF的每个页面转换为单独的PDF的要求,更新了代码以与具有可变的页面数量的PDF一起使用。

Here's a clean code that does the same. Tested on my machine.

import os
import shutil
import time
from pathlib import Path

from pikepdf import Pdf

# assuming team and geo are present in your project dir
temp_path = Path(".").joinpath("temp")
geo_path = Path(".").joinpath("geo")

# splitting up PDFs to singel Page PDFs
# https://www.thepythoncode.com/article/split-pdf-files-in-python
print("###Split up PDFs\n")

for root, directories, files in os.walk(temp_path):
    for file in files:
        file_path = temp_path.joinpath(file)
        print(f'processing file {file_path}')

        # HERE
        with Pdf.open(file_path) as pdf:
            file2pages = {i: [i, i+1] for i in range(len(pdf.pages))}
            for pdf_index, pages_range in file2pages.items():
                new_pdf = Pdf.new()
                [new_pdf.pages.append(pdf.pages[i]) for i in range(*pages_range)]
                output_path = geo_path.joinpath(str(pdf_index))
                output_path.mkdir(parents=True, exist_ok=True)
                out_filename = output_path.joinpath(f'{file}-{pdf_index}.pdf')
                print(f'saving {pages_range[0] + 1}-{pages_range[1]} pages as {out_filename}')
                new_pdf.save(out_filename)

# deleting temp folder
print()
print("#Removing temp folder...")
shutil.rmtree(temp_path)
time.sleep(2)

Your code seems to have numerous issues. Hence re-implemented it. Observe the usage of with

if you still get error, you might want to check the file permissions on your machine.

EDIT: Based on the requirement to convert each page of the pdf into a separate pdf, updated the code to work with pdfs having variable number of pages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文