I have a .exe program that is converting an AGS file to an Excel file.
I have a lot of files to convert so I'm wondering can I automate this with Python? The convert-program only requires 2 locations, one for the input and one for the output, see the picture.
With Python I'm albe to start the program with the following code, but is it possible to pass the 2 path's and click Export.
import subprocess
path = r'C:\Program Files (x86)\AGSToExcel.exe'
#Start the programs
subprocess.call([path])
#other locations
path_ags = r'C:\Users\X\Documents\AGS_file1.ags'
path_excel = r'C:\Users\X\Documents\Book1.xls'
I'm fairly new with package subprocess so I have not a clue if this is even possible.
You can probably use pyautogui to click and type if needed.
You can see a simple example here.
PyAutoGUI EXAMPLE
Related
i am trying to make a zoom bot with pyautogui, I have got it working fine in pycharm/running it from the cmd but when i try to run it double clicking the main file everything breaks down. Basically the code is searching for one image using the locateCenterOnScreen() function and clicks it.
# Opening the zoom App
os.popen('C:\\Users\\user\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe')
time.sleep(3)
# Click on join a meeting
join = pyautogui.locateCenterOnScreen('JoinMeeting.png', grayscale=True)
pyautogui.moveTo(join)
pyautogui.click()
time.sleep(3)
So, everything works as intended when on pycharm and when on the folder that the main is running on cmd (using python main.py)
Yet, if I double click the main, it opens the cmd and then crashes saying it cannot locate the image on the locateCenterOnScreen function. My initial thought was that if you double click, it doesn't run where the .png file is and thus doesn't find it, but this can't be because when I run it through cmd it finds the picture perfectly.
I am trying to turn this whole project into .exe to make it usable for more people and this is why i need it to work with double click, because when I turn it into a .exe it will behave the same way.
Thank you
when you double click it uses the default "open with" option, and that might not be python. Python programs need to be opened with python.exe. I believe the default is python launcher.
But if you want to make it into an executable file (.exe), all you need is cx-Freeze. It is an extremely useful tool that turns python codes into exe files. In the end, the effect of double clicking shouldn't matter.
I have cx-Freeze 3.4, and here's a video that explains how to download and use it.
https://www.youtube.com/watch?v=HosXxXE24hA
To address your issue, you could easily change your code to no longer depend on the current work directory but instead locate the files relatively to the path of your script.
Here is an example:
import os
# Get the absolute path of the Python script and extract the directory (dirname)
here = os.path.dirname(os.path.abspath(__file__))
# Concatenate the path with the image name
join_meeting_path = os.path.join(here, 'JoinMeeting.png')
# Opening the zoom App
os.popen('C:\\Users\\user\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe')
time.sleep(3)
# Click on join a meeting
# Use the variable to be independent of the location.
join = pyautogui.locateCenterOnScreen(join_meeting_path, grayscale=True)
pyautogui.moveTo(join)
pyautogui.click()
time.sleep(3)
Let me know if it works for you.
I'm trying to extract the icons from all of the shortcuts in the Start Menu folder. So far I've managed to walk the directory tree, and I just need something to extract the icon from each shortcut. I've tried a few methods suggested across the internet, but I can't seem to make it work fully.
Method 1: Using a program called ResourcesExtract through os.system() to extract the icon from the .lnk file. I soon discovered that this doesn't work for .lnk files, only .exe or .dlls.
import os
os.system(f"resourcesextract.exe /source {shortcut}")
Method 2: Extracting the icon file from the targets of the shortcuts (which can be obtained quite easily using the pywin32 library) using ResourcesExtract. Unfortunately, this only works for some of the programs, due to some shortcuts pointing to .exes without icons.
import os
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
target = shell.CreateShortCut(shortcut).TargetPath
os.system(f"resourcesextract.exe /source {target}")
Method 3: Using pywin32 to get the icon directory. This only works for around 120 of the 300 shortcuts I need it to work on.
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
icon, status = str(shell.CreateShortCut(shortcut).IconLocation).split(",")
I also came across a way to do it using the .NET framework, but I don't know how to interface that with python or if it will even work.
Is anyone aware of a method to extract icons from .lnk files in Python that works on all shortcuts?
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
ShortCut = shell.CreateShortCut('example.lnk')
icon_location = ShortCut.IconLocation
It work for me.
So I want to add a button in my text area in Spotfire which will open an excel file (that is connected to my spotfire visualisation) or at least to a network folder with that file.
I believe I can write an ironpython script just to open that file and make changes. How will I do that?
Update:
After some googling I have tried to run a simple script smth like:
t=open('D:/data/folderA/folderB/file.xlsx','w')
To avoid problems with "/" or "\", I also tried importing os
import os
t=open('D:','data', 'folderA', 'folderB', 'file.xlsx', 'w')
Neither of these work.
For those who is still struggling to find the solution, it turned out to be simpler than I thought it would.
from System.Diagnostics import Process
Process.Start(r 'start c:\test\abc.xlsx')
I am trying to code a very basic code where I copy some text from one program and then paste it in a different program. I'm not sure how to do this as Pyperclip only seems to paste the text on the command window where I run the code. I want to be able to click on the text-editing program and then have my code paste the text there. I'm attaching my code
import pyperclip
import time
pyperclip.copy('testing')
time.sleep(5)
pyperclip.paste()
When I run this code nothing actually happens. It doesn't paste anything, not even on the command window. I have the sleep function there because that's when I take the time to click on the text-editing program so that Python pastes the text there but it doesn't work.
If all you want to do is copy content to another text editor, then try using pyautogui module. This module allows mouse/keyboard automation via python code.
Code:
import pyautogui
import time
time.sleep(5)
a = "testing"
pyautogui.typewrite(a)
The above code will start typing the word testing after 5 seconds of program execution, so you will have to open your text editor during that duration.
The best part (or the worst) about pyautogui module is that it is focus independent i.e. it works regardless of whether the current application has focus control or not.
Just a sidenote don't use pyperclip for copying/changing/accessing clipboard data, rather try win32clipboard, if you're on windows, as it allows a lot better control over the clipboard.
Although this is a rather old question the answer cost me several hours. My goal was to input a number into another program. That program however is autocompleting the input and therefore using typewrite() (write() in the current version of pyautogui) leads to unexpected behaviour.
However pyautogui helped me in the end, together with pyperclip. Here is the code I am using:
import pyperclip
import time
pyperclip.copy('hello')
time.sleep(5)
with pyautogui.hold('ctrl'):
pyautogui.press(['v'])
This solution is dirty, but it works.
In older versions of SPSS, one could ask for the path to a file using a syntax similar to this:
filename = GetFilePath("*.txt","txt", ,"Specify input file",0)
How can I achieve the same result using recent versions of SPSS, that rely on Python?
I found the answer. So similarly to how the VBA in .SBS script works, Python can use a library directly from SPSS to show a file input dialog.
BEGIN PROGRAM.
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
print(filename)
END PROGRAM.
sbs/wwd scripts are still supported on Statistics.
As for the prompt, you could create a wwd/sbs dialog to prompt for the file name and then open it, or, without using scripting, you could create a custom dialog box using the Custom Dialog Builder (on Utilities) containing a Browse control and issue a GET command.
If you want this to happen when Statistics is launched, you can create a startup script or use the STATS PROJECT extension command, depending on exactly what you want to do.
I am not aware of SPSS Statistics ever having a function such as GetFilePath, but you can use the Python spssaux.getDatasetInfo function for this, e.g., in a program write
import spssaux
filename = spssaux.getDatasetInfo()