Issue with Python tkinter / pypdftk / subprocess(?) - python-3.x

I have been using this whole script flawlessly on my PC. I attempted to put it on my coworkers PC, but this particular part doesn't seem to work. I am using a tkinter interface to take data from psql and fill a premade fillable PDF using pypdftk, then either saving it using asksaveasfilename and opening it with subprocess.Popen or not saving it and opening it as a temp file using subprocess.run. On my PC both work great. On coworkers PC, neither work.
On my coworkers PC, the save option opens the save dialog with all the correct info as far as I can tell, and lets me go through the process of saving a file as it normally would, but then the file just doesn't save and never actually appears. If I open as a temp file, it throws the exception.
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
import tkinter.messagebox
import pypdftk
from tkinter.filedialog import asksaveasfilename
import os.path
import os
import subprocess
from pathlib import Path
def file_handler(form_path, data, fname):
try:
tl2 = tk.Toplevel()
tl2.wm_title('File Handler')
w = 340
h = 55
ws = tl2.winfo_screenwidth()
hs = tl2.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
tl2.geometry('%dx%d+%d+%d' % (w, h, x, y))
def save_and_open():
savefile = asksaveasfilename(defaultextension=".pdf", initialdir="C:\\Desktop", filetypes=[('pdf file', '*.pdf')], initialfile=fname)
generated_pdf = pypdftk.fill_form(form_path, data, savefile)
subprocess.Popen(generated_pdf,shell=True)
def open_without_save():
try:
generated_pdf = pypdftk.fill_form(form_path, data)
os.rename(generated_pdf, generated_pdf+".pdf")
generated_pdf = generated_pdf+".pdf"
subprocess.run(generated_pdf,shell=True)
except:
tk.messagebox.showerror("Unable to open", "An error has occurred. Please try again.")
else:
tl2.destroy()
finally:
if os.path.exists(generated_pdf):
os.remove(generated_pdf)
print("Successfully removed temp file.")
save = tk.Button(tl2,text='Save and Open', width=20, command=save_and_open)
nosave = tk.Button(tl2,text='Open without saving', width=20,command=open_without_save)
save.grid(row=0, columnspan=2, sticky='NESW', padx=5, pady=10, ipadx=5, ipady=5)
nosave.grid(row=0, column=2, columnspan=2, sticky='NESW', padx=5, pady=10, ipadx=5, ipady=5)
tl2.mainloop()
except:
tk.messagebox.showerror("Unable to open", "An error has occurred. Please try again.")
As far as I can tell, everything works until you get into the save_and_open and open_without_save functions. I left in all the libraries I believe are relevant.
I should also mention, I am quite a novice at python. So if any of this is ugly coding, feel free to shame me for it.
update:
I now believe the problem to be here in the pypdftk.py file:
if os.getenv('PDFTK_PATH'):
PDFTK_PATH = os.getenv('PDFTK_PATH')
else:
PDFTK_PATH = '/usr/bin/pdftk'
if not os.path.isfile(PDFTK_PATH):
PDFTK_PATH = 'pdftk'
My error states pdftk is not a known command. My guess is that there is no environment variable, then it looks to the /usr/bin and cannot find the pdftk file, so it's just making "pdftk" a string? I don't know much about /usr/bin, but is there a way to check that?

What's going on in lines 19-21
if os.getenv('PDFTK_PATH'): is checking to see if the environment variable PDFTK_PATH even exists on your machine. If so, the constant PDFTK_PATH is set to the value provided by the PDFTK_PATH environment key/variable.
Otherwise, it sets PDFTK_PATH to /usr/bin/pdftk. Two things are happening here... First, it provides a path to the binary, i.e., /usr/bin. Second, it provides the name of the binary, i.e., pdftk. In other words, it sets PDFTK_PATH to path + executable filename.
(NOTE: The directory usr/bin is where most executables are stored on machines running Unix-like operating systems, e.g., Mac, Ubuntu, etc. This is alluded to in the repo, see here.)
To err on the side of caution, if not os.path.isfile(PDFTK_PATH): checks to see if the pdftk binary can indeed be found in the /usr/bin/ folder. If not, it sets PDFTK_PATH to pdftk, i.e., it sets the path to the pdftk binary to the very directory in which pypdftk.py is located.
Farther down, the try block runs a test call on whatever the value of PDFTK_PATH was ultimately set to. See lines 46-49. If the binary, pdftk.exe, is not where PDFTK_PATH says it is, then you get the error that you got.
Concerning the r-string
As for casting the string literal to an r-string, that actually did nothing. The r prefix simply redefines the \ as just that, a backslash, rather than allowing the \ to continue to function as cue to an escape sequence. See here and here. You'll notice that neither /usr/bin/pdftk nor pdftk, i.e., where you prepended the r, contain any backslashes.
What I think happened...
After you took the advice of acw1668, and installed the PDF Toolkit (pdftk); and reinstalled the pypdftk package, you had probably fixed the problem. I don't know, but maybe you had not restarted your IDE/editor during these steps? (Side note: Sometimes you need to restart your IDE/editor after changes have been made to your machine's environment variables/settings.)
The short answer: If you're on a windows machine, the install of the pdftk toolkit added PDFTK_PATH to your environment; or, if you're on a Unix-based machine, the install placed the binary in the /usr/bin directory.
Regardless, I assure you the r had nothing to do with it. Now that you know it's working, let's prove it... take out the r, you'll see that it is still working.

I was able to fix this problem by going into the pypdftk.py file and changing the paths to raw strings like such:
if os.getenv('PDFTK_PATH'):
PDFTK_PATH = os.getenv('PDFTK_PATH')
else:
PDFTK_PATH = r'/usr/bin/pdftk'
if not os.path.isfile(PDFTK_PATH):
PDFTK_PATH = r'pdftk'

Related

Tkvideo is working on one file but not an exact copy

Alright, guys, I'm in need of some serious help here. I am running python3 on a mac, using a virtual environment in vs code. So to the point, I have a python project where I am opening a window and playing a video on loop here is the code
from tkinter import *
import numpy
from tkvideo import tkvideo
from PIL import Image, ImageTk
import os
root = Tk()
my_label = Label(root)
root.geometry("500x500")
root.configure(bg='black')
root.resizable(False, False)
my_label.pack()
my_label.config(borderwidth=0)
player = tkvideo("path/test.mp4", my_label, loop = 1, size = (600,338))
# root.iconbitmap('path/Logo.png')
root.title('Elvis')
ico = Image.open('Logo.png')
photo = ImageTk.PhotoImage(ico)
root.wm_iconphoto(False, photo)
player.play()
root.mainloop()
it works fine and runs with no issues, the problem comes in at my other python project.
I have everything the exact same but with different file locations.It does run but the video doesn't play, the UI looks different, and the logo is replaced with a standard python logo. If you were wondering yes, I do have all the same packages installed and have even tried uninstalling and reinstalling the packages. anyways this is the error RuntimeError: No ffmpeg exe could be found. Install ffmpeg on your system, or set the IMAGEIO_FFMPEG_EXE environment variable. i am new to python but i have a pretty good grasp on how it works. I just don't see how it would work on one but not on another identical one with the same packages. If anyone more advanced than me could help it would be greatly appreciated. I am sorry if there is a super obvious answer to this. I have worked for a good 4 hours and can't seem to figure it out. anyways ---thank you

Beginner Python Project - My function is skipping, what is causing this?

as the title says I'm a beginner with Python. I have started to work on what I thought first was a simple enough script for scanning a folder and printing the names of each subdirectory to a CMD prompt.
However, I've run into an issue where the function in the code below does not execute. I suspect it's to do with Windows permissions which is why I've added in the is_admin(): function.
My question is, what is it that is causing the function to skip? and what is the proper way to achieve what it is I am trying to do?
Any and all help is appreciated and if anyone could point me in the direction for learning more about Python and the Windows OS technical side for programmers would be doing me a huge favor.
Thanks in advance :)
import os, sys, ctypes
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
rootdir = 'C:/Windows'
def listdirs(rootdir):
for file in os.listdir(rootdir):
d = os.path.join(rootdir, file)
if os.path.isdir(d):
print(d)
listdirs(d)
listdirs(rootdir)
else:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv\[1:\]), None, 1)
input('Press any key to continue...')
Expecting the program to:
Produce an output of all the subdirectories of a folder printed to a CMD prompt window and have the window stay open when the program has finished executing each line of code. The Window should remain open until the user is finished with it. The Windows UAC should prompt asking the user if they wish to run as admin, if the user is already an admin then it should skip this and proceed to run the program.

Using Python to control system audio i [Voice Control]

I'm writing a program to control my system volume either increase or decrease in voice command. but I don't know how and what are all the packages should to be installed? I'm using python 3.7.0 in pycharm
I am not sure but u can probably use tkinter. I had created a duplication GUI file so just copy this code and paste in python:
from tkinter import *
try:
root=Tk()
root.title("file copier")
fname=mystring=StringVar()
root.geometry = Canvas(root, width = 3000, height = 3000)
label=Label(root,text="enter the file name with file extension", fg="black") .grid(row=0, column=0,sticky=W)
entry=Entry(root, textvariable=fname) .grid(row=0, column=1)
#specifying the function going to be assigned to the button
def duplicatefunction():
print(fname.get())
with open(fname.get(),"rb") as f:
data = f.read()
with open("copy file(rename this file and copy info inside).exe","wb") as f:
f.write(data)
button1=Button(text="duplicate file", command=duplicatefunction) .grid(row=0, column=2)
root.mainloop()
except FileNotFoundError:
print("no such file found named", entry)
so if u see i have typed exe extension after file name in the function. Try typing all ur code in a notepad and then convert to exe from this file(after pasting the code save the file and run it), u can do that from changing extension but u should make copy too, anyway this file's code was only for ur reference and btw the copy file will be in pycharm only, if u use it for python IDLE like me it will come in file explorer. convert a notepad file from this by duplicating the file and then go back, type ur code for the volume and use this code at the end for the code to control volume:
fname=StringVar()
print(fname.get())
with open(fname.get(),"rb") as f:
data = f.read()
so the data should be read and work, i hope it does cuz it works for me, in other projects.
I found something interesting on https://techoverflow.net/2020/04/04/how-to-set-windows-audio-volume-using-python/
but this only works for Windows.
The package they are using is pycaw. You can install it with
pip install pycaw.
That is the script on the website:
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import math
# Get default audio device using PyCAW
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
# Get current volume
currentVolumeDb = volume.GetMasterVolumeLevel()
volume.SetMasterVolumeLevel(currentVolumeDb - 6.0, None)
# NOTE: -6.0 dB = half volume !
If I use
volume.SetMasterVolumeLevel(-65.25, None)
my system volume is set to 0
and with
volume.SetMasterVolumeLevel(0, None)
I can set the system volume to 100

Sublime Text 3: confirm to delete file

Is there a way to confirm deleting a file from the tree (left hand side) or remove the option from the context menu?
It is too easy to miss i.e. rename and click delete file instead. Then the file is gone.
I googled and found it should be moved to the trash folder but either that doesn't apply to Win7 or to using network drives. As a result the files are actually deleted or moved somewhere I have failed to track them down so far.
Using Sublime Text (build 3083)
Important: take a look at iron77 answer. It says that if you modify Default.sublime-package (options 1 and 3) this changes might be overriden if sublime text is updated.
Option 1: modify side_bar.py file
You can use sublime API to show an ok/cancel dialog. The code you are looking for is in a file called side_bar.py. This file is located inside the zip file Default.sublime-package. In windows this is usually located in C:\Program Files\Sublime Text 3\Packages\Default.sublime-package and can be explored using programs such as WinRar.
Inside that file locate DeleteFileCommand and add this 3 new lines, so it is changed from this:
class DeleteFileCommand(sublime_plugin.WindowCommand):
def run(self, files):
# Import send2trash on demand, to avoid initialising ctypes for as long as possible
import Default.send2trash as send2trash
To this
class DeleteFileCommand(sublime_plugin.WindowCommand):
def run(self, files):
isSure = sublime.ok_cancel_dialog('Are you sure you want to delete the file?')
if isSure != True:
return
# Import send2trash on demand, to avoid initialising ctypes for as long as possible
import Default.send2trash as send2trash
We are showing a ok/cancel dialog and if the user doesn't press Ok then we return and the file isn't removed.
Notes:
You will have to add the same code in class DeleteFolderCommand in order to confirm also when deleting folders.
Is a good practice to backup your Default.sublime-package file first just in case something goes wrong. EDIT: use a different folder for the backup or the package could be loaded twice causing problems as the OP has said in his comment.
As this is python code indentation is extremly important, don't
replace any spaces for tabs nor add any extra space or it will not
work (you can see it console).
Result:
Option 2: use an existing package
As user leesei said in his answer you can use SideBarEnhancements package to achieve your goal. This package adds many other features to the file context menu as you can see in the following image, but it is a very good choice as you only need to install an exsiting package.
Option 3: remove option from context menu
Edit Side Bar.sublime-menu inside Default.sublime-package (see option 1) and remove this line (and if you want remove also the line reffering to deleting folders):
{ "caption": "Delete File", "command": "delete_file", "args": {"files": []} },
While sergioFC's answers work great, I'm bit worried of modifying Default.sublime-package, as it might someday get overwritten when Sublime is updated, so the fix would need to be manually re-applied after each such update. SideBarEnhancements, on the other hand, might have too many features for someone who only wants the confirmation when deleting a file.
Alternatively, you can add a simple confirmation dialog that should be more resistant to ST updates, by creating a file (plugin). On Linux it should be somewhere around ~/.config/sublime-text-3/Packages/User/confirm_delete.py, and if you're on Windows/Mac or this path does not work for you, you can simply choose from the top menu: Tools -> Developer -> New Plugin and later save as confirm_delete.py - thanks to harrrrrrry for this suggestion. Code to put in:
from Default.side_bar import *
class DeleteFileCommand(sublime_plugin.WindowCommand):
def run(self, files):
if len(files) == 1:
message = "Delete File %s?" % files[0]
else:
message = "Delete %d Files?" % len(files)
if sublime.ok_cancel_dialog(message, "Delete") != True:
return
# Import send2trash on demand, to avoid initialising ctypes for as long as possible
import Default.send2trash as send2trash
for f in files:
v = self.window.find_open_file(f)
if v != None and not v.close():
return
send2trash.send2trash(f)
def is_visible(self, files):
return len(files) > 0
This code is basically a copy of DeleteFileCommand function from Default.sublime-package's side_bar.py combined with confirmation dialogs from DeleteFolderCommand from the same file, as Sublime has such dialog natively for folder removal.
When I choose delete by right clicking on a file in the SideBar, I get a confirmation.
Maybe it's SideBarEnhancements. It is worth a try.
WTF a software that doesn't have a confirm dialog before delete. I can't believe this. Sad but true. Just stupid software!
Unfortunately there is no way to activate a confirmation. Usually the the deleted file is moved to the trash folder but as you mentioned this is only true for local files. Files on a shared network drive are still deleted immediately. This is a Windows 'feature' :(
Locally the Recycle Bin is part of Windows Explorer -- and on the network you are NOT dealing with explorer on the server. Explorer locally isn't going to copy the file to the user's workstation just to put it into the recycle bin.
You CAN implement Microsofts Shadow Copy however, then users can undelete and compare versions. This would be the only way so far for network drives until the sublime developer decides to make an optional confirmation dialog.
According to #iron77's answer, the path for plugin could not exist (in my case). An easier way is:
1) Click Sublime Text topbar menu Tools -> Developer -> New Plugin.
2) Paste the snippet
from Default.side_bar import *
class DeleteFileCommand(sublime_plugin.WindowCommand):
def run(self, files):
if len(files) == 1:
message = "Delete File %s?" % files[0]
else:
message = "Delete %d Files?" % len(files)
if sublime.ok_cancel_dialog(message, "Delete") != True:
return
# Import send2trash on demand, to avoid initialising ctypes for as long as possible
import Default.send2trash as send2trash
for f in files:
v = self.window.find_open_file(f)
if v != None and not v.close():
return
send2trash.send2trash(f)
def is_visible(self, files):
return len(files) > 0
3) Save as confirm_delete.py.

Write to %temp% with python?

I have a small program that works fine on my PC but I want to make it portable. What it does is download an image from the internet, set as desktop background, wait one minute and update the image. I know that I cannot write directly to folders like appdata, as I do not know the username of the person using the computer. I need to save the downloaded image somewhere, so I would save it in the windows Temp folder.
Some options I think would be to (However I don't know how to do this in python)
Use something like %temp% to access the folder.
Find out the username of the person running the program and insert into path
Use a variable for the username.
Use relative paths
I would like to try and not have to use another module not by default included in Python 3, as I want to cx_freeze it later on.
import pythoncom
from urllib import request
from win32com.shell import shell, shellcon
from time import sleep
def get_image():
f = open('C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg', 'wb') #Open old image
f.write(request.urlopen('blalbla.com/foo/img.jpg').read()) #Get new image and write
f.close()
pathtoimg = 'C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg'
count = 0
while 1:
get_image()
iad = pythoncom.CoCreateInstance(shell.CLSID_ActiveDesktop, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IActiveDesktop)
iad.SetWallpaper(pathtoimg, 0)
iad.ApplyChanges(shellcon.AD_APPLY_ALL)
count += 1
print(count)
sleep(60)
Use this to locate Temp:
import os
mytmpdir = os.environ['TEMP'] #Must be uppercase

Resources