简体   繁体   English

尝试从 Django 视图中使用 win32com.client 获取 COM 对象时出现 com_error

[英]com_error when trying to get COM object with win32com.client from Django view

I am trying to connect a SAP GUI session to my Django project so I can interact with it, by using win32com.client to work with COM objects.我正在尝试将 SAP GUI 会话连接到我的 Django 项目,以便通过使用 win32com.client 与 COM 对象进行交互。 When working from the shell I have no problem at all to get this working by running the following code:在 shell 中工作时,我完全没有问题,可以通过运行以下代码来使其正常工作:

sap_gui = win32com.client.GetObject("SAPGUI")
application = sap_gui.GetScriptingEngine
connection = application.Children(0)
session = connection.Children(0)

If I start the server with this code in the views.py Django module this also works, and I can get the session info displayed in my Django view.如果我在views.py Django 模块中使用此代码启动服务器,这也有效,并且我可以在我的 Django 视图中显示会话信息。 However, I want to be able to connect and disconnect such session by hand, since different users will need to connect to different sessions, and by running the code at start I can only stick to the first session.但是,我希望能够手动连接和断开此类会话,因为不同的用户需要连接到不同的会话,并且通过在开始时运行代码,我只能坚持第一个会话。

I have been trying to get this working by defining the following view in views.py:我一直在尝试通过在 views.py 中定义以下视图来使其工作:

def dashboard(request):
  if request.method == 'POST' and 'SAP_button' in request.POST:
    # Get the COM object (SAP session in this case)
    sap_gui = win32com.client.GetObject("SAPGUI") # ERROR HERE
    application = sap_gui.GetScriptingEngine
    connection = application.Children(0)
    session = connection.Children(0)
    # This is just a test to see if I get the desired output from the COM object
    test = session.info.User
  return render(request, 'Users/dashboard.html', {'test': test})

The corresponding html code for the form ('Users/dashboard.html') is the following:表单('Users/dashboard.html')对应的html代码如下:

<form action="", method="POST">{% csrf_token %}
    <button type="submit", name="SAP_button">Connect SAP</button>
</form>

When clicking the button the request works as expected but I get the following com_error : (-2147221020, 'Invalid syntax', None, None) .单击按钮时,请求按预期工作,但我收到以下com_error : (-2147221020, 'Invalid syntax', None, None) This error comes from the very first line when trying to get the SAP COM object: sap_gui = win32com.client.GetObject("SAPGUI") .此错误来自尝试获取 SAP COM 对象时的第一行: sap_gui = win32com.client.GetObject("SAPGUI")

It looks like the code that is run from a view can't access the COM object but I am pretty new to Django and even researching for such error I have not been able to understand why this is happening or a possible solution/workaround.看起来从视图运行的代码无法访问 COM 对象,但我对 Django 还很陌生,甚至在研究此类错误时我也无法理解为什么会发生这种情况或可能的解决方案/解决方法。 Any help is appreciated.任何帮助表示赞赏。

After some more research I ended up finding out the code to solve this issue.经过更多研究,我最终找到了解决此问题的代码。 After a POST request the Django view is run in a different thread, and therefore COM libraries need to be initialized for such thread.在 POST 请求之后,Django 视图在不同的线程中运行,因此需要为此类线程初始化 COM 库。 I solved this issue by adding the following line of code before getting the COMobject:我通过在获取 COMobject 之前添加以下代码行解决了这个问题:

import pythoncom
...
pythoncom.CoInitialize()
sap_gui = win32com.client.GetObject("SAPGUI") # ERROR SOLVED
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM