Here’s the code for my reprex:
JavaScript
x
30
30
1
from PyPDF2 import PdfFileReader as rdr
2
from PyPDF2 import PdfFileWriter as wtr
3
4
import os
5
6
SELF_PATH = os.path.dirname(__file__)
7
file_name = 'pdf_file'
8
9
in_path = fr'{SELF_PATH}{file_name}.pdf'
10
assert os.path.isfile(in_path)
11
input = open(in_path, 'r+b')
12
13
reader = rdr(input, strict=False)
14
writer = wtr()
15
16
orientation = reader.getPage(0).get('/Rotate')
17
18
for pagenum in range(reader.numPages):
19
page = reader.getPage(pagenum)
20
page.rotateClockwise(180)
21
writer.addPage(page)
22
23
out_path = fr'{SELF_PATH}_{file_name}.pdf'
24
assert os.path.isfile(out_path)
25
output = open(out_path, 'wb')
26
27
writer.write(output)
28
output.close()
29
input.close()
30
From this, I used auto-py-to-exe to build a one-directory .exe. Then I opened cmd as an administrator, navigated to the .exe’s folder and attempted to run the executable. Here’s the output:
JavaScript
1
5
1
Traceback (most recent call last):
2
File "tab.py", line 11, in <module>
3
PermissionError: [Errno 13] Permission denied: 'C:\Users\paulo\OneDrive\Pastas\Python\Personal\DocReader\tab\pdf_file.pdf'
4
[25204] Failed to execute script 'tab' due to unhandled exception!
5
I am all out of ideas at this point, and I’d appreciate any thoughts.
Advertisement
Answer
I solved the issue. My antivirus was blocking file creation.
Additionally, I’d suggest sticking with python’s standard syntax regarding files.
JavaScript
1
6
1
import os
2
3
SELF_PATH = os.path.dirname(__file__)
4
with open(f'{SELF_PATH}/{file_name}.pdf', 'wb') as output:
5
output.write(output)
6