pdfkit [WinError 740] The requested operation requires elevation python3 - python-3.x

I would like to convert an HTML page into a PDF file, based on the given URL. I have tried pdfkit, but it throws the following error:
[WinError 740] The requested operation requires elevation.
Code:
import pdfkit
path_wkthmltopdf = "D:\\w.exe"
config = pdfkit.configuration(wkhtmltopdf = path_wkthmltopdf )
pdfkit.from_url("http://www.google.com", 'd:\\out.pdf', configuration=config)
Output error:
n [42]: import pdfkit
path_wkthmltopdf = "D:\\w.exe"
config = pdfkit.configuration(wkhtmltopdf = path_wkthmltopdf )
pdfkit.from_url("http://www.google.com", 'd:\\out.pdf', configuration=config)
Traceback (most recent call last):
File "<ipython-input-42-58323936ac63>", line 5, in <module>
pdfkit.from_url("http://www.google.com", 'd:\\out.pdf', configuration=config)
File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\site-packages\pdfkit\api.py", line 26, in from_url
return r.to_pdf(output_path)
File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\site-packages\pdfkit\pdfkit.py", line 129, in to_pdf
stderr=subprocess.PIPE)
File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 143, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\subprocess.py", line 1017, in _execute_child
startupinfo)
OSError: [WinError 740] The requested operation requires elevation

I encountered with this problem too, I solved it by running .exe after run this it will create new dir, go inside this dir then bin dir you will find a exe file there like
C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe
So, replace "D:\w.exe" to above kind of path in your code then it will work

Cause
You are most likely getting that error as a result of downloading the Installer version, but not installig it. Thus, when running your code with wkhtmltopdf in the configuration pointing to that executable (which requires elevation of privilege), you get the following error:
OSError: [WinError 740] The requested operation requires elevation
Solution
You could install wkhtmltopdf by running that Installer version and choosing the destination folder (let's say C:\Program Files). You will find the wkhtmltopdf.exe file that you need to add to your configuration inside the bin folder. Hence, you should use as follows:
config = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
pdfkit.from_url('http://google.com', 'out.pdf', configuration=config)
Another solution would be to download the 7z Archive version, extract the files, in which you'll find wkhtmltopdf.exe under the bin folder as well.

I ran into this issue when I had not gone through the full installation of the wkpdftohtml library. Once it was unpacked, this ran without need for elevation.

Related

Reading .doc files in python on windows 10

Note: This was flagged as a potential duplicate of this, but the point of my question is that using textract doesn't work. I am looking either for (a) a way to get textract to work on windows 10 or (b) an alternate solution.
I am building a system that needs to read various types of files. I have set up pdfminer to read the .pdfs, and based on the process outlined here I installed textract, and I can now also read .docx files. However textract relies on antiword for reading .doc files and I cannot get this to work, even after following the directions here I could not find and install a working version of antiword. I do not have microsoft word installed on my machine, and I am running windows 10 with python 3.6.5. Is there any other way to read .doc files?
Here is the bug when running textract.process('d.doc') (ignore the first error, the file is definitely there):
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\utils.py", line 84, in run
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\__init__.py", line 77, in process
return parser.process(filename, encoding, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\utils.py", line 46, in process
byte_string = self.extract(filename, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\doc_parser.py", line 9, in extract
stdout, stderr = self.run(['antiword', filename])
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\utils.py", line 91, in run
' '.join(args), 127, '', '',
textract.exceptions.ShellError: The command antiword d.doc failed with exit code 127
From the 'Windows installation problem' series: https://github.com/deanmalmgren/textract/issues/194#issuecomment-507243521
After following the steps to 'install' antiword, I was facing the same problem as yours.
Restarting windows after setting up the environment path variable entirely solved this exact error message for me. (This was the last error I was facing for handling .doc files with textract)
Installation instructions extracted from https://github.com/deanmalmgren/textract/issues/194#issuecomment-506065817
"Install Antiword
(I followed Reading .doc file in Python using antiword in Windows (also .docx))
Go to https://www.softpedia.com/get/Office-tools/Other-Office-Tools/Antiword.shtml
Extract to c:\antiword (yes it's required to be in the root directory)
Add location to path like we did tesseract-ocr [basically add c:\antiword to the system path (environment variable)]"
I was able to get part of the text using olefile, but olefile ultimately only handles bytes and does not handle the encoding of Word .doc files. The solution is to use LibreOffice, see my other question here

Can't find the specified file

I'm trying to convert every file in a folder from mp4 to mp3 using the libary pydub but when i do it return the error FileNotFoundError: [WinError 2] The system cannot find the file specified. I'm not sure how i can be more specific about the file.
Here's my code:
from pydub import AudioSegment
import os
import os.path
folder = 'C:/Users/Magnus/Desktop/test/'
videos = []
for file in [f for f in os.listdir(folder) \
if os.path.isfile(os.path.join(folder, f))]:
AudioSegment.from_file(folder + file).export(folder + file, format="mp3")
Inside of C:/Users/Magnus/Desktop/test/ i have files that look like this M83_-_Bibi_The_Dog_(Audio).mp4
Full traceback:
Traceback (most recent call last):
File "C:\Users\Magnus\Desktop\youtube-playlist-downloader.py", line 64, in <module>
AudioSegment.from_file(os.path.join(folder, file)).export(os.path.join(folder, file), format="mp3")
File "C:\Users\Magnus\AppData\Local\Programs\Python\Python35\lib\site-packages\pydub\audio_segment.py", line 505, in from_file
p = subprocess.Popen(conversion_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "C:\Users\Magnus\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Users\Magnus\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
also replacing folder + file with os.path.join(folder, file) doesn't work either
I had a similar error.I was using anaconda.
This error doesn't look like a file error but due to either :
ffmpeg not being installed.
(or)
ffmpeg not being added to the environment variable.(Along with FFmpeg even your python.exe and conda.exe should be in the env variable)
Do the following steps :
On Windows:
Download and extract libav/ffmpeg from Windows binaries.
Add the libav /bin folder to your PATH environment variable.
pip install pydub
You can use this for reference : https://github.com/jiaaro/pydub

MoviePy Error: The system cannot find the file specified

I'm getting the error in the title when trying to run the example code below on Windows 10/Python 3.6.1/Sublime Text Editor 3. I made sure to set my MAGICK_HOME environment variable to point to where I manually installed ImageMagick. I made sure to only have 1 version of ImageMagick installed. I made sure FFMPEG, Numpy, imageio, Decorator, and tqdm were all installed.
Yes, the file "vidclip.mp4" exists. The desired behavior here is to simply clip the video and add text to the center as per the example on the github page, i.e. to simply run the code successfully.
Does anyone know what might be wrong?
Example code from github:
from moviepy.editor import *
video = VideoFileClip("vidclip.mp4").subclip(7,64)
# Make the text. Many more options are available.
txt_clip = ( TextClip("Ken Block who?",fontsize=70,color='white')
.set_position('center')
.set_duration(10) )
result = CompositeVideoClip([video, txt_clip]) # Overlay text on video
result.write_videofile("vidclip_edited.webm",fps=25) # Many options...
The full error trace:
Traceback (most recent call last):
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\site-packages\moviepy\video\VideoClip.py", line 1220, in __init__
subprocess_call(cmd, verbose=False )
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\site-packages\moviepy\tools.py", line 42, in subprocess_call
proc = sp.Popen(cmd, **popen_params)
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\av\Desktop\Desktop\Projects\Youtube\blender\test\testMoviePy.py", line 6, in <module>
txt_clip = ( TextClip("Ken Block who?",fontsize=70,color='white')
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\site-packages\moviepy\video\VideoClip.py", line 1229, in __init__
raise IOError(error)
OSError: MoviePy Error: creation of None failed because of the following error:
[WinError 2] The system cannot find the file specified.
.This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or.that the path you specified is incorrect
Found the problem. I didn't realize it was imperative that I specify the location of ffmpeg.exe and convert.exe within ImageMagick's config_defaults.py file. After that, I install MoviePy (in that order).

Cx_Freeze compiled pygame file doesn't work

The following error happens if i try to compile my python (using python 3.2) file:
Traceback(most recent call last):
File
"c:\python32\lib\site-packages\cx_Freeze\initscripts\Console3.py", line
27, in <module>
exec(code, m.__dict__)
File "Abertura.py", line 208, in <module>
File "Abertura.py", line 154, in main
File "Abertura.py", line 9, in __init__
pygame.error: Couldn't open
C:\Python32\build\exe.win32-3.2\library.zip\Imagens\menu1.png
I already included pygame._view and tried to copy the 'Imagens' directory to the library.zip file, but it doesn't work. I'm using images, musics and videos that come's from other directories by including in my code:
def file_path(filename, directory):
return os.path.join(
os.path.dirname(os.path.abspath(__file__)),
directory,
filename
)
And this is my setup.py file:
from cx_Freeze import setup, Executable
exe=Executable(
script="Abertura.py",
base="Win32Gui",
)
includefiles=[('C:\Python32\Imagens', 'Imagens'),
('C:\Python32\Musicas','Musicas'),
('C:\Python32\Videos','Videos')
]
includes=[]
excludes=[]
packages=[]
setup(
version = "1.0",
description = "RPG",
author = "Pedro Forli e Ivan Veronezzi",
name = "Batalha Inapropriada",
options = {'build_exe': {'excludes':excludes,'packages':packages,'include_files':includefiles}},
executables = [exe]
)
How do i fix it?
(sorry about my possibles english mistakes)
Anything accessed from within a compressed archive (such as zips, rars, tar.gzs, etc...) need to be decompressed first before you access them.
That being said, you should not have your recourse files in a zip because decompressing it every time to want to access something is slow, and difficult. Your resource files should be in a normal directory, not an archive.
The reason why you're getting this error is because it's looking for a folder named library.zip and it's not finding one because library.zip is not a folder, it's a file.
How I would suggest to combat this error is to extract everything into a folder named library and to change in your code anywhere that library.zip exists to library.

Error while opening subprocess python

I get this error while trying to open a txt. file. Please tell how to fix it .
code: subprocess.Popen("/home/yash/Documents/ct.txt")
time.sleep(1)
error:
Traceback (most recent call last):
File "/home/yash/Documents/final_downloader22.py", line 5, in <module>
subprocess.Popen("/home/yash/Documents/ct.txt")
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
Text files are normally not executable. What do you mean with "open" here?
This could mean "open it so that I can use it in the program".
This works with file_object = open('/home/yash/Documents/ct.txt', 'r') and then using that file_object.
This as well could mean "let the file type's associated application open the file so that the user can edit it".
In this case, you would have to find out what the associated application is. How you do this is dependent on the OS you use as well as the window manager/desktop environment. It cannot be answered in a few short sentences.
You could use webbrowser module to open a text file for editing in a portable manner in a script:
import webbrowser
webbrowser.open("/home/yash/Documents/ct.txt")
From a command-line:
$ python -mwebbrowser "/home/yash/Documents/ct.txt"
See also patches for "Add shutil.open" Python issue.

Resources