Python win32com: Internet Explorer COM object ? (used to work?) - python

I have this very simple program:
from win32com import client
ie=client.Dispatch("InternetExplorer.Application")
This used to work (I think I broke something when I re-used 'makepy.py' to try and add in constants for IE).
It still works on another machine where I haven't been so slap-dash with 'makepy.py'.
Here's what I get in an interactive Python session on the non-working machine:
>>> ie
>>> <win32com.gen_py.Microsoft Internet Controls.IWebBrowser2 instance at 0x14701432
>
>>> ie.Visible=True
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python26\lib\site-packages\win32com\client\__init__.py", line 471, in
__setattr__
self._oleobj_.Invoke(*(args + (value,) + defArgs))
pywintypes.com_error: (-2147352573, 'Member not found.', None, None)
And the same on a working machine:
>>> from win32com import client
>>> ie=client.Dispatch("InternetExplorer.Application")
>>> ie
>>> <ComObject InternetExplorer.Application>
>>> ie.Visible=1 # This then brings up IE correctly.
How do I get this working again ? Or am I using the wrong way of launching IE ?
Thanks !

Went into here:
Python26\Lib\site-packages\win32com\gen_py
Renamed the .py and .pyc file to .py_ and .pyc_ files :
85CC894D-5673-4868-9A22-9E15B7E694D3x0x1x1.pyc
Restarted Python: now get the Internet Explorer. phew...

Related

netCDF4 - Python error

Can anyone tell me what I did wrong? I am using python-conda, and the files I have from http://meop40.troja.mff.cuni.cz:11180/gw.projekt/data.stratopauza/netcdf.profily/
Why it tells me that file doesn't exist?
>>> import netCDF4
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> url = 'http://meop40.troja.mff.cuni.cz:11180/gw.projekt/data.stratopauza/netcdf.profily/atmPrf_C001.2010.227.00.03.G04_2013.3520_nc'
>>> nc = netCDF4.Dataset(url)
**syntax error, unexpected WORD_WORD, expecting SCAN_ATTR or SCAN_DATASET or SCAN_ERROR
context: <!DOCTYPE^ HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL /gw.projekt/data.stratopauza/netcdf.profily/atmPrf_C001.2010.227.00.03.G04_2013.3520_nc.dds was not found on this server.</p><hr><address>Apache/2.4.12 (Ubuntu) Server at meop40.troja.mff.cuni.cz Port 11180</address></body></html>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "netCDF4\_netCDF4.pyx", line 1811, in netCDF4._netCDF4.Dataset.__init__ (netCDF4\_netCDF4.c:12626)
IOError: NetCDF: file not found**
NetCDF4.Dataset() can only access remote NetCDF files which are served by an OPeNDAP service, which can return metadata about the file. The error message returned is incorrect and misleading.
There is a brief tutorial, which mentions this and gives basic information at: http://unidata.github.io/netcdf4-python/#section1
I downloaded the file and had no problem opening the file. You should use the method in the answer to your previous question https://stackoverflow.com/a/44622713/1211981
Update:
Go to:
http://meop40.troja.mff.cuni.cz:11180/gw.projekt/data.stratopauza/netcdf.profily/
Click one or more of the links and save to a folder where you will run your script. Change your script or python commands to:
>>> url = 'atmPrf_C001.2010.227.00.03.G04_2013.3520_nc'
>>> nc = netCDF4.Dataset(url)
netCDF4.Dataset() will take either a url or a local file name and work the same way. In this case it will recognize the file as a NetCDF / OPeNDAP compatible.

python sqlite3 connection path with spaces

I am trying to connect with a full path but I get this problem
>>> path = "/home/astro/Fun LAB/DBlist"
>>> db = sql.connect(path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: unable to open database file
>>>
i 've also tried this
>>> path = "/home/astro/Fun\ LAB/DBlist"
>>> db = sql.connect(path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: unable to open database file
>>>
I know it's because of the whitespace cause I've tried this and it works
>>> path = "/home/astro/DBlist"
>>> db = sql.connect(path)
>>>
so is there is an easy way to escape the whitespaces in the path or am I doing something wrong?
You might try the uri style approach - it worked for me in getting past connection issues due to spaces in file paths with sqlite3:
Escaping the spaces with %20
prefixing the path with file:///
Examples:
file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
file:///C:/Documents%20and%20Settings/fred/Desktop/
I would suggest naming files or folders without spaces. (I don't think it's possible either) Instead, use dash or underscores.
The next problem is to see where you're running your Python interpreter. If it's nested inside a directory and you're trying to create a path from the outside, then your link would raise an error.
>>> path = "/home/astro/Fun LAB/DBlist"
or
>>> path = "/home/astro/Fun\ LAB/DBlist"
or
>>> path = "/home/astro/DBlist"
wouldn't work if you run python inside these directories or from another.
To correct this, try:
>>> path = "../home/path/to/file"
Having spaces is indeed a problem.
I would suggest bring a symlink and work with that:
!ln -sf '$path_to_db_file' .
db = sql.connect('db_file')

win32com passing parameter as reference

I'm struggeling to get win32com.client working with Solidworks 2014.
Here is my minimum example:
import win32com.client
model_path = 'MW_011_500.SLDDRW'
ret_val1 = 0
ret_val2 = 0
sw_app = win32com.client.Dispatch('SLDWORKS.Application')
sw_app.OpenDoc6(model_path, 1, 1, "", ret_val1, ret_val2)
I'm trying to automatize Solidwork with the help of python. I managed to get the comclient running and the server is answering the command to open a document. BUT I get this error:
Traceback (most recent call last):
File "test.py", line 6, in <module>
sw_app.OpenDoc6(model_path, 1, 1, "", ret_val1, ret_val2)
File "<COMObject SLDWORKS.Application>", line 2, in OpenDoc6
pywintypes.com_error: (-2147352571, 'Typkonflikt.', None, 5)**
The API function I'm using is documented here:
http://help.solidworks.com/2012/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.isldworks~opendoc6.html
They error I receive, tell's me it's the wrong type of parameter I'm using. On the fith place of the call.
But is there a way to use win32com with reference parameter? If so, how? Or can't it work? I couldn't find any solutions yet.
Old question, but maybe you're still interested- use OpenDoc to pass the path and the file type (http://help.solidworks.com/2018/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.isldworks~opendoc.html) or OpenDoc7 (for SW 2018 and later) and pass in only what you need with an open doc specification.
Otherwise you'd need to pass in variables by reference, which in my research Python doesn't allow.

Adding new portgroups to vmware virtual switches using pysphere

I'm trying to automate the addition of new portgroups to ESXi hosts using pysphere. I'm using the following code fragment:
from pysphere import MORTypes
from pysphere import VIServer, VIProperty
from pysphere.resources import VimService_services as VI
s = VIServer()
s.connect(vcenter, user, password)
host_system = s.get_hosts().keys()[17]
prop = VIProperty(s, host_system)
propname = prop.configManager._obj.get_element_networkSystem()
vswitch = prop.configManager.networkSystem.networkInfo.vswitch[0]
network_system = VIMor(propname, MORTypes.HostServiceSystem)
def add_port_group(name, vlan_id, vswitch, network_system):
request = VI.AddPortGroupRequestMsg()
_this = request.new__this(network_system)
_this.set_attribute_type(network_system.get_attribute_type())
request.set_element__this(_this)
portgrp = request.new_portgrp()
portgrp.set_element_name(name)
portgrp.set_element_vlanId(vlan_id)
portgrp.set_element_vswitchName(vswitch)
portgrp.set_element_policy(portgrp.new_policy())
request.set_element_portgrp(portgrp)
s._proxy.AddPortGroup(request)
However, when I attempt to run it, I get the following error:
>>> add_port_group(name, vlan_id, vswitch, network_system)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 12, in add_port_group
File "/usr/lib/python2.6/site-packages/pysphere-0.1.8- py2.6.egg/pysphere/resources/VimService_services.py", line 4344, in AddPortGroup
response = self.binding.Receive(AddPortGroupResponseMsg.typecode)
File "/usr/lib/python2.6/site-packages/pysphere-0.1.8- py2.6.egg/pysphere/ZSI/client.py", line 545, in Receive
return _Binding.Receive(self, replytype, **kw)
File "/usr/lib/python2.6/site-packages/pysphere-0.1.8- py2.6.egg/pysphere/ZSI/client.py", line 464, in Receive
raise FaultException(msg)
pysphere.ZSI.FaultException: The object has already been deleted or has not been completely created
I've attempted to swap in different values for "vswitch" and "network_system", but I haven't had any success. Has anyone attempted to do something similar with pysphere successfully?
I can accomplish what I need through Powershell, which demonstrates that it isn't a vmware issue, but I don't want to use Powershell in this particular case.
I tried your code on one of our vSpheres.
It seems you are passing the object to set_element_vswitchName rather than the name. Maybe this will help:
vswitch = prop.configManager.networkSystem.networkInfo.vswitch[0].name

AttributeError: 'module' object has no attribute 'Describe'

I am very new to Python. Reading up on new material for class, I had to copy and paste a few examples to see how things work. So, I copied and pasted code from this website ( https://www.e-education.psu.edu/geog485/node/54 ) under the topic 'Looping in GIS Models'.
Here's mine:
import arcpy
try:
arcpy.env.workspace = "C:\Users\dan and kathryn\Desktop\school\programming\Lesson1"
fcList = arcpy.ListFeatureClasses()
for featureClass in fcList:
arcpy.CopyFeatures_management (featureClass, "C:\Users\dan and kathryn\Desktop\school\programming\Lesson 2\PracticeData/" + featureClass)
except:
print "Script failed to complete"
print arcpy.GetMessages(2)
Here's the error msg:
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonWin\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\dan and kathryn\Desktop\loops_gis.py", line 1, in <module>
import arcpy
File "C:\Users\dan and kathryn\Desktop\arcpy.py", line 5, in <module>
desc = arcpy.Describe(featureClass)
AttributeError: 'module' object has no attribute 'Describe'
Contacted my teacher and he wrote code and sent it to me but everything matches. All of the backslashes, indentations, everything except for folder locations. Not sure what is going on but he suspects this problem runs beyond what I've written in my code.
I've already had to alter the site-packages folder within pythonWin and it now contains:
# .pth file for the PyWin32 extensions
win32
win32\lib
Pythonwin
C:\Program Files (x86)\ArcGIS\Desktop10.1\bin
C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy
C:\Program Files (x86)\ArcGIS\Desktop10.1\ArcToolbox\Scripts
Not sure what's going on or why 'Describe' is even popping up in the problems
Whenever writing path string, use r"path with backslash". It will take care of the backslash and forward slash problem. Use 'os' module when, the paths needed to be joined.
heres what i had the change (all in the backslashes, I had no idea):
arcpy.env.workspace = "C:/Users/dan and kathryn/Desktop/school/programming/Lesson1"
AND
arcpy.CopyFeatures_management (featureClass, "C:\Users\dan and kathryn\Desktop\school\programming\Lesson 2\PracticeData/" + featureClass)

Categories

Resources