Is it possible to use win32com in python script for GIMP - python

I try to use win32com in GIMP python script.
Script should appear under tab: /Grzegorz.
I noticed that "IMPORT FROM EXCEL..." shortcut appears on tab only when I delete import declaration "import win32com.client as win32". So I assume win32com causes problem.
Win32com works when I try it outside gimp as a python file so win32com is ok.
#!/usr/bin/env python
from gimpfu import *
import win32com.client as win32
def import_text_my(img):
Excel = win32.gencache.EnsureDispatch("Excel.Application")
Excel.Visible = True
title = Excel.ActiveSheet.Cells(Excel.Selection.Row, 1).Value
description = Excel.ActiveSheet.Cells(Excel.Selection.Row, 1).Value
param = pdb.gimp_image_get_active_layer(img)
ret = param.Split(" ")
if len(ret) == 1:
ret = [title, title, 'textZwyklyLeft']
output = param.replace(ret[1], title)
layerMy = img.active_layer
layerMy.Name = output
pdb.gimp_text_layer_set_text(layerMy, description)
register(
"python_import_excel",
"IMPORT FROM EXCEL",
"import_text",
"Grzegorz Mackiewicz",
"Grzegorz Mackiewicz",
"2019",
"IMPORT FROM EXCEL...",
"",
[
(PF_IMAGE, "img", "Input image", None)
],
[],
import_text_my, menu="<Image>/Grzegorz")
main()
Does anyone know if there is possiblility to use win32com in gimp script?

pypiwin32Yes, but you would have to add the win32com module to the python runtime used by Gimp.
Some indications to install things here (this is for numpy, but holds for anything installed with PIP).
This post hints that you should install pypiwin32 with PIP.

Related

How do I specify the path to the library to run a script from another script?

I need to check the change of one parameter, if it has changed - I need to restart the script.
code:
import subprocess, os.path, time
from engine.db_manager import DbManager
DB = DbManager(os.path.abspath(os.path.join('../webserver/db.sqlite3')))
tmbot = None
telegram_config = DB.get_config('telegram')
old_telegram_token = ''
vkbot = None
vk_config = DB.get_config('vk')
old_vk_token = ''
while True:
telegram_config = DB.get_config('telegram')
if old_telegram_token != telegram_config['token']:
if vkbot != None:
tmbot.terminate()
tmbot = subprocess.Popen(['python', 'tm/tm_bot.py'])
old_telegram_token = telegram_config['token']
print('telegram token was updated')
vk_config = DB.get_config('vk')
if old_vk_token != vk_config['token']:
if vkbot != None:
vkbot.terminate()
vkbot = subprocess.Popen(['python', 'vk/vk_bot.py'])
old_vk_token = vk_config['token']
print('vk token was updated')
time.sleep(30)
I get errors:
enter image description here
While there might be subtle differences between unix and windows, the straight-up answer is: you can use PYTHONPATH environment variable to let python know where to look for libraries.
However, if you use venv, I'd recommend activating it first, or calling the relevant binary instead of setting the environment variable.
Consider this scenario: you have a venv at /tmp/so_demo/venv, and you try to run this file:
$ cat /tmp/so_demo/myscript.py
import requests
print("great success!")
Running the system python interpreter will not find the requests module, and will yield the following error:
$ python3 /tmp/so_demo/myscript.py
Traceback (most recent call last):
File "/tmp/so_demo/myscript.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
As I have installed requests in the venv, if I provide the path to python, it will know where to look:
$ PYTHONPATH='/tmp/so_demo/venv/lib/python3.8/site-packages/' python3 /tmp/so_demo/myscript.py
great success!
but using the binary inside the venv is better, as you don't need to be familiar with the internal venv directory paths, and is much more portable (notice that the path I provided earlier depicts the minor version):
$ /tmp/so_demo/venv/bin/python /tmp/so_demo/myscript.py
great success!

.py file not running properly in py.exe, works in eclipse

Hi I have a simple python script that uses an odbc driver to connect to a database get a dataframe and store it/overwrite an excel file. When I run the script using eclipse, it works just fine. However, when I run it by right clicking the .py file and open with py.exe, the excel file is not being overwritten/saved.
Ultimately, I want other users to be able to install python and just double click a .py script to update an excel file. Does anyone know why it is not working with the right click method? They should both be using the same interpreter when I checked.
import pyodbc
import pandas as pd
cnxn = pyodbc.connect('Driver={ODBC Driver (x64)};'
'DSN=MyDSN;'
'Server=ServerAddress;'
'Database=Stuff;')
t1 = "table1"
sql = ("select * " + "from " + t1)
writer = pd.ExcelWriter("MyExcelFile.xlsx")
dframe = pd.read_sql(sql,cnxn)
aggDf = dframe.groupby(['DEPARTMENT']).sum()
dframe.to_excel(writer,"RawSalesData", index = False)
aggDf.to_excel(writer, "SalesStats")
writer.save()
writer.close()
Below is the results of running the sys code suggested by Jacob in the comment. Seems like both methods match.
sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0)
[
'C:\\Users\\persona\\PythonWorkSpace\\TestPython',
'C:\\Users\\persona\\anaconda3',
'C:\\Users\\persona\\anaconda3\\DLLs',
'C:\\Users\\persona\\anaconda3\\libs',
'C:\\Users\\persona\\anaconda3\\pkgs',
'C:\\Users\\persona\\anaconda3\\conda-meta',
'C:\\Users\\persona\\anaconda3\\envs',
'C:\\Users\\persona\\anaconda3\\etc',
'C:\\Users\\persona\\anaconda3\\include',
'C:\\Users\\persona\\anaconda3\\Lib',
'C:\\Users\\persona\\anaconda3\\Library',
'C:\\Users\\persona\\anaconda3\\man',
'C:\\Users\\persona\\anaconda3\\Menu',
'C:\\Users\\persona\\anaconda3\\Scripts',
'C:\\Users\\persona\\anaconda3\\share',
'C:\\Users\\persona\\anaconda3\\sip',
'C:\\Users\\persona\\anaconda3\\tcl',
'C:\\Users\\persona\\anaconda3\\Tools',
'C:\\WINDOWS\\system32',
'C:\\Users\\persona\\Anaconda3\\python36.zip',
'C:\\Users\\persona\\Anaconda3\\lib\\site-packages',
'C:\\Users\\persona\\Anaconda3\\lib\\site-packages\\win32',
'C:\\Users\\persona\\Anaconda3\\lib\\site-packages\\win32\\lib',
'C:\\Users\\persona\\Anaconda3\\lib\\site-packages\\Pythonwin'
]
^ right click method
--------------------
sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0)
[
'C:\\Users\\persona\\PythonWorkSpace\\TestPython',
'C:\\Users\\persona\\PythonWorkSpace\\TestPython',
'C:\\Users\\persona\\anaconda3',
'C:\\Users\\persona\\anaconda3\\DLLs',
'C:\\Users\\persona\\anaconda3\\libs',
'C:\\Users\\persona\\anaconda3\\pkgs',
'C:\\Users\\persona\\anaconda3\\conda-meta',
'C:\\Users\\persona\\anaconda3\\envs',
'C:\\Users\\persona\\anaconda3\\etc',
'C:\\Users\\persona\\anaconda3\\include',
'C:\\Users\\persona\\anaconda3\\Lib',
'C:\\Users\\persona\\anaconda3\\Library',
'C:\\Users\\persona\\anaconda3\\man',
'C:\\Users\\persona\\anaconda3\\Menu',
'C:\\Users\\persona\\anaconda3\\Scripts',
'C:\\Users\\persona\\anaconda3\\share',
'C:\\Users\\persona\\anaconda3\\sip',
'C:\\Users\\persona\\anaconda3\\tcl',
'C:\\Users\\persona\\anaconda3\\Tools',
'C:\\Program Files\\eclipse',
'C:\\Users\\persona\\Anaconda3\\lib\\site-packages',
'C:\\Users\\persona\\Anaconda3\\lib\\site-packages\\win32',
'C:\\Users\\persona\\Anaconda3\\lib\\site-packages\\win32\\lib',
'C:\\Users\\persona\\Anaconda3\\lib\\site-packages\\Pythonwin',
'C:\\Users\\persona\\Anaconda3\\python36.zip'
]
Make a python file like this:
from sys import version_info, path
print(version_info)
print(path)
Then try opening with both methods you mentioned. The results should match, and if they don't then there is your issue. If they do match, you should add more logging/exception handling so we can see why this fails.

import python module when launching from VBA

It is the first time I write as I really didn't find any solution to my issue.
I want to allow my user to launch some Python program from Excel.
So i have this VBA code at some point:
lg_ErrorCode = wsh.Run(str_PythonPath & " " & str_PythonArg, 1, bl_StopVBwhilePython)
If lg_ErrorCode <> 0 Then
MsgBox "Couldn't run python script! " _
+ vbCrLf + CStr(lg_ErrorCode)
Run_Python = False
End If
str_PythonPath = "C:\Python34\python.exe C:\Users\XXXX\Documents\4_Python\Scan_FTP\test.py"
str_PythonArg = "arg1 arg2"
After multiple testing, the row in error in Python is when I try to import another module (I precise that this VBA code is working without the below row in Python):
import fct_Ftp as ftp
The architecture of the module is as follow:
4_Python
-folder: Scan_FTP
- file: test.py (The one launch from VBA)
-file: fct_Ftp.py
(For information, I change the architecture of the file, and try to copy the file at some other position just to test without success)
The import has no problem when I launch Test.py directly with:
import sys, os
sys.path.append('../')
But from VBA, this import is not working.
So I figured out this more generic solution, that dont work as well from Excel/VBA
import sys, os
def m_importingFunction():
str_absPath = os.path.abspath('')
str_absPathDad = os.path.dirname(str_absPath)
l_absPathSons = [os.path.abspath(x[0]) for x in os.walk('.')]
l_Dir = l_absPathSons + [str_absPathDad]
l_DirPy = [Dir for Dir in l_Dir if 'Python' in Dir]
for Dir in l_DirPy:
sys.path.append(Dir)
print(Dir)
m_importingFunction()
try:
import fct_Ftp as ftp
# ftp = __import__ ("fct_Ftp")
write += 'YAAAA' # write a file YAAAA from Python
except:
write += 'NOOOOOO' # write a file NOOOOO from VBA
f= open(write + ".txt","w+")
f.close()
Can you please help me as it is a very tricky questions ?
Many thanks to you guys.
You are able to start your program from the command line?
Why not create a batch file with excel which you then start in a shell?

xlwings runpython EOL error

I have recently installed xlwings on my Mac and am currently trying to write a small programme to update some data(via requests). As a test, I tried to update the cryptocurrency prices via an API and write them into excel.
Without using runpython, the code works. However as soon as I run my VBA code,
I get this error:
File "<string>", line 1
import sys, os;sys.path.extend(os.path.normcase(os.path.expandvars('/Users/Dennis/Documents/crypto;
^
SyntaxError: EOL while scanning string liberal
I have searched numerous threads and forums, but can't seem to find an answer to my problem.
For a better understanding,
my python code:
import requests, json
from datetime import datetime
import xlwings as xw
def do():
parameter = {'convert' : 'EUR'}
#anfrage über API
query_ticker = requests.get('https://api.coinmarketcap.com/v1/ticker', params = parameter)
#anfragedaten in JSON-format
data_ticker = query_ticker.json()
wb = xw.Book.caller()
ws0 = wb.sheets['holdings']
for entry in data_ticker:
# update eth price
if entry['symbol'] == 'ETH':
ws0.range('B14').value = float(entry['price_eur'])
#update btc price
if entry['symbol'] == 'BTC':
ws0.range('B15').value = float(entry['price_eur'])
if entry['symbol'] == 'NEO':
ws0.range('B16').value = float(entry['price_eur'])
if entry['symbol'] == 'XRP':
ws0.range('B17').value = float(entry['price_eur'])
now = datetime.now()
write_date = '%s.%s.%s' %(now.day, now.month, now.year)
write_time = '%s:%s:%s' %(now.hour, now.minute,now.second)
ws0.range('B2').value = write_date
ws0.range('B3').value = write_time
wb.save('holdings.xlsm')
#wb.close()
this is my vba code:
Sub update_holdings()
RunPython ("import update_holdings; update_holdings.do()")
End Sub
Solved this. I just wanted to post the solution for anyone who might be confronted with the same issue.
I went to check my xlwings.conf file, in order to see the setup for "INTERPRETER" and "PYTHONPATH". I never did editing on this, however, it was formatted incorrectly.
The correct format is:
"INTERPRETER","pythonw"
"PYTHONPATH",""
My config file was setup this way:
"PYTHONPATH","
"
"INTERPRETER","Python"
Also, the path to my python was set incorrectly by default. Even though my command line works with Anaconda python 3.6, "pythonw" used the interpreter set in .bash_profile referenced to python 2.7 which came pre-installed with macOS.
Editing the config file "INTERPRETER" solved this issue.
Thanks everyone.

How do I use cx_freeze?

I've created my setup.py file as instructed but I don't actually.. understand what to do next. Typing "python setup.py build" into the command line just gets a syntax error.
So, what do I do?
setup.py:
from cx_Freeze import setup, Executable
setup(
name = "On Dijkstra's Algorithm",
version = "3.1",
description = "A Dijkstra's Algorithm help tool.",
exectuables = [Executable(script = "Main.py", base = "Win32GUI")])
Add import sys as the new topline
You misspelled "executables" on the last line.
Remove script = on last line.
The code should now look like:
import sys
from cx_Freeze import setup, Executable
setup(
name = "On Dijkstra's Algorithm",
version = "3.1",
description = "A Dijkstra's Algorithm help tool.",
executables = [Executable("Main.py", base = "Win32GUI")])
Use the command prompt (cmd) to run python setup.py build. (Run this command from the folder containing setup.py.) Notice the build parameter we added at the end of the script call.
I'm really not sure what you're doing to get that error, it looks like you're trying to run cx_Freeze on its own, without arguments. So here is a short step-by-step guide on how to do it in windows (Your screenshot looks rather like the windows command line, so I'm assuming that's your platform)
Write your setup.py file. Your script above looks correct so it should work, assuming that your script exists.
Open the command line (Start -> Run -> "cmd")
Go to the location of your setup.py file and run python setup.py build
Notes:
There may be a problem with the name of your script. "Main.py" contains upper case letters, which might cause confusion since windows' file names are not case sensitive, but python is. My approach is to always use lower case for scripts to avoid any conflicts.
Make sure that python is on your PATH (read http://docs.python.org/using/windows.html)1
Make sure are are looking at the new cx_Freeze documentation. Google often seems to bring up the old docs.
I ran into a similar issue. I solved it by setting the Executable options in a variable and then simply calling the variable. Below is a sample setup.py that I use:
from cx_Freeze import setup, Executable
import sys
productName = "ProductName"
if 'bdist_msi' in sys.argv:
sys.argv += ['--initial-target-dir', 'C:\InstallDir\\' + productName]
sys.argv += ['--install-script', 'install.py']
exe = Executable(
script="main.py",
base="Win32GUI",
targetName="Product.exe"
)
setup(
name="Product.exe",
version="1.0",
author="Me",
description="Copyright 2012",
executables=[exe],
scripts=[
'install.py'
]
)
You can change the setup.py code to this:
from cx_freeze import setup, Executable
setup( name = "foo",
version = "1.1",
description = "Description of the app here.",
executables = [Executable("foo.py")]
)
I am sure it will work. I have tried it on both windows 7 as well as ubuntu 12.04
find the cxfreeze script and run it. It will be in the same path as your other python helper scripts, such as pip.
cxfreeze Main.py --target-dir dist
read more at:
http://cx-freeze.readthedocs.org/en/latest/script.html#script
I usually put the calling setup.py command into .bat file to easy recall.
Here is simple code in COMPILE.BAT file:
python setup.py build
#ECHO:
#ECHO . : ` . * F I N I S H E D * . ` : .
#ECHO:
#Pause
And the setup.py is organized to easy customizable parameters that let you set icon, add importe module library:
APP_NAME = "Meme Studio"; ## < Your App's name
Python_File = "app.py"; ## < Main Python file to run
Icon_Path = "./res/iconApp48.ico"; ## < Icon
UseFile = ["LANGUAGE.TXT","THEME.TXT"];
UseAllFolder = True; ## Auto scan folder which is same level with Python_File and append to UseFile.
Import = ["infi","time","webbrowser", "cv2","numpy","PIL","tkinter","math","random","datetime","threading","pathlib","os","sys"]; ## < Your Imported modules (cv2,numpy,PIL,...)
Import+=["pkg_resources","xml","email","urllib","ctypes", "json","logging"]
################################### CX_FREEZE IGNITER ###################################
from os import walk
def dirFolder(folderPath="./"): return next(walk(folderPath), (None, None, []))[1]; # [ Folder ]
def dirFile(folderPath="./"): return next(walk(folderPath), (None, None, []))[2]; # [ File ]
if UseAllFolder: UseFile += dirFolder();
import sys, pkgutil;
from cx_Freeze import setup, Executable;
BasicPackages=["collections","encodings","importlib"] + Import;
def AllPackage(): return [i.name for i in list(pkgutil.iter_modules()) if i.ispkg]; # Return name of all package
#Z=AllPackage();Z.sort();print(Z);
#while True:pass;
def notFound(A,v): # Check if v outside A
try: A.index(v); return False;
except: return True;
build_msi_options = {
'add_to_path': False,
"upgrade_code": "{22a35bac-14af-4159-7e77-3afcc7e2ad2c}",
"target_name": APP_NAME,
"install_icon": Icon_Path,
'initial_target_dir': r'[ProgramFilesFolder]\%s\%s' % ("Picox", APP_NAME)
}
build_exe_options = {
"includes": BasicPackages,
"excludes": [i for i in AllPackage() if notFound(BasicPackages,i)],
"include_files":UseFile,
"zip_include_packages": ["encodings"] ##
}
setup( name = APP_NAME,
options = {"build_exe": build_exe_options},#"bdist_msi": build_msi_options},#,
executables = [Executable(
Python_File,
base='Win32GUI',#Win64GUI
icon=Icon_Path,
targetName=APP_NAME,
copyright="Copyright (C) 2900AD Muc",
)]
);
The modules library list in the code above is minimum for workable opencv + pillow + win32 application.
Example of my project file organize:
========== U P D A T E ==========
Although cx_Freeze is a good way to create setup file. It's really consume disk space if you build multiple different software project that use large library module like opencv, torch, ai... Sometimes, users download installer, and receive false positive virus alert about your .exe file after install.
Thus, you should consider use SFX archive (.exe) your app package and SFX python package separate to share between app project instead.
You can create .bat that launch .py file and then convert .bat file to .exe with microsoft IExpress.exe.
Next, you can change .exe icon to your own icon with Resource Hacker: http://www.angusj.com/resourcehacker/
And then, create SFX archive of your package with PeaZip: https://peazip.github.io/
Finally change the icon.
The Python Package can be pack to .exe and the PATH register can made with .bat that also convertable to .exe.
If you learn more about command in .bat file and make experiments with Resource Hacker & self extract ARC in PeaZip & IExpress, you can group both your app project, python package into one .exe file only. It'll auto install what it need on user machine. Although this way more complex and harder, but then you can custom many install experiences included create desktop shorcut, and install UI, and add to app & feature list, and uninstall ability, and portable app, and serial key require, and custom license agreement, and fast window run box, and many more,..... but the important features you get is non virus false positive block, reduce 200MB to many GB when user install many your python graphic applications.

Categories

Resources