Skip to content
Advertisement

How to rename PDF file, with texts extracted from the PDF file?

I am trying to use Python to rename PDF file using part of the file content. Here is the situation.

The PDF file is a commercial invoice, contains wordings “Commercial Invoice” and “Department”. I want to rename the file to “Commercial Invoice” and ” Department “, such as “353624 HR”.

Here is what I have so far:

JavaScript

it works until print out the text extracted ‘ print final_name’, but at the last part when renaming the file, it gives an error ” WindowsError: [Error 32] The process cannot access the file because it is being used by another process”.

What went wrong here? it seems the file was once not closed properly?

Advertisement

Answer

in def getPDFContent(path), after p = file(path, "rb"), when the content has been copied, you need to close the file.

JavaScript

put this just after the for loop but in the function.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement