• <small id='H2w4j'></small><noframes id='H2w4j'>

    <i id='H2w4j'><tr id='H2w4j'><dt id='H2w4j'><q id='H2w4j'><span id='H2w4j'><b id='H2w4j'><form id='H2w4j'><ins id='H2w4j'></ins><ul id='H2w4j'></ul><sub id='H2w4j'></sub></form><legend id='H2w4j'></legend><bdo id='H2w4j'><pre id='H2w4j'><center id='H2w4j'></center></pre></bdo></b><th id='H2w4j'></th></span></q></dt></tr></i><div id='H2w4j'><tfoot id='H2w4j'></tfoot><dl id='H2w4j'><fieldset id='H2w4j'></fieldset></dl></div>
  • <legend id='H2w4j'><style id='H2w4j'><dir id='H2w4j'><q id='H2w4j'></q></dir></style></legend>

        <bdo id='H2w4j'></bdo><ul id='H2w4j'></ul>
      <tfoot id='H2w4j'></tfoot>

      1. 无法在 Python 上使用 win32com 完全关闭 Excel

        时间:2023-09-11

        1. <tfoot id='60ZZf'></tfoot>
            <bdo id='60ZZf'></bdo><ul id='60ZZf'></ul>
            • <small id='60ZZf'></small><noframes id='60ZZf'>

                <legend id='60ZZf'><style id='60ZZf'><dir id='60ZZf'><q id='60ZZf'></q></dir></style></legend>
                  <tbody id='60ZZf'></tbody>

                  <i id='60ZZf'><tr id='60ZZf'><dt id='60ZZf'><q id='60ZZf'><span id='60ZZf'><b id='60ZZf'><form id='60ZZf'><ins id='60ZZf'></ins><ul id='60ZZf'></ul><sub id='60ZZf'></sub></form><legend id='60ZZf'></legend><bdo id='60ZZf'><pre id='60ZZf'><center id='60ZZf'></center></pre></bdo></b><th id='60ZZf'></th></span></q></dt></tr></i><div id='60ZZf'><tfoot id='60ZZf'></tfoot><dl id='60ZZf'><fieldset id='60ZZf'></fieldset></dl></div>
                • 本文介绍了无法在 Python 上使用 win32com 完全关闭 Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这是我的代码,我为 VBA、.NET 框架找到了很多答案,并且是很奇怪.当我执行此操作时,Excel 将关闭.

                  This is my code, and I found many answers for VBA, .NET framework and is pretty strange. When I execute this, Excel closes.

                  from win32com.client import DispatchEx
                  excel = DispatchEx('Excel.Application')
                  wbs = excel.Workbooks
                  wbs.Close()
                  excel.Quit()
                  wbs = None
                  excel = None # <-- Excel Closes here
                  

                  但是当我执行以下操作时,它并没有关闭.

                  But when I do the following, it does not close.

                  excel = DispatchEx('Excel.Application')
                  wbs = excel.Workbooks
                  wb = wbs.Open('D:\Xaguar\A1.xlsm')
                  wb.Close(False)
                  wbs.Close()
                  excel.Quit()
                  wb = None
                  wbs = None
                  excel = None  # <-- NOT Closing !!!
                  

                  我在 Stack Overflow 问题中找到了一些可能的答案 Excel 进程在互操作后保持打开状态;传统方法不起作用.问题是那不是 Python,我没有找到 Marshal.ReleaseComObjectGC.我查看了 ...site-packages/win32com 和其他网站上的所有演示.

                  I found some possible answer in Stack Overflow question Excel process remains open after interop; traditional method not working. The problem is that is not Python, and I don't find Marshal.ReleaseComObject and GC. I looked over all the demos on ...site-packages/win32com and others.

                  如果我能得到 PID 并杀死它,即使它不会打扰我.

                  Even it does not bother me if I can just get the PID and kill it.

                  我在 根据窗口名杀死进程(win32).

                  可能不是正确的方法,但解决方法是:

                  May be not the proper way, but a workround is:

                  def close_excel_by_force(excel):
                      import win32process
                      import win32gui
                      import win32api
                      import win32con
                  
                      # Get the window's process id's
                      hwnd = excel.Hwnd
                      t, p = win32process.GetWindowThreadProcessId(hwnd)
                      # Ask window nicely to close
                      win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
                      # Allow some time for app to close
                      time.sleep(10)
                      # If the application didn't close, force close
                      try:
                          handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
                          if handle:
                              win32api.TerminateProcess(handle, 0)
                              win32api.CloseHandle(handle)
                      except:
                          pass
                  
                  excel = DispatchEx('Excel.Application')
                  wbs = excel.Workbooks
                  wb = wbs.Open('D:\Xaguar\A1.xlsm')
                  wb.Close(False)
                  wbs.Close()
                  excel.Quit()
                  wb = None
                  wbs = None
                  close_excel_by_force(excel) # <--- YOU #@#$# DIEEEEE!! DIEEEE!!!
                  

                  推荐答案

                  试试这个:

                  wbs.Close()
                  excel.Quit()
                  del excel # this line removed it from task manager in my case
                  

                  这篇关于无法在 Python 上使用 win32com 完全关闭 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:无法使用 PyQt4 将参数传递给 ActiveX COM 对象 下一篇:如何在 Python 中导入 COM 对象命名空间/枚举?

                  相关文章

                  最新文章

                    <small id='DUpGU'></small><noframes id='DUpGU'>

                    <tfoot id='DUpGU'></tfoot>

                    • <bdo id='DUpGU'></bdo><ul id='DUpGU'></ul>
                  1. <legend id='DUpGU'><style id='DUpGU'><dir id='DUpGU'><q id='DUpGU'></q></dir></style></legend>

                      <i id='DUpGU'><tr id='DUpGU'><dt id='DUpGU'><q id='DUpGU'><span id='DUpGU'><b id='DUpGU'><form id='DUpGU'><ins id='DUpGU'></ins><ul id='DUpGU'></ul><sub id='DUpGU'></sub></form><legend id='DUpGU'></legend><bdo id='DUpGU'><pre id='DUpGU'><center id='DUpGU'></center></pre></bdo></b><th id='DUpGU'></th></span></q></dt></tr></i><div id='DUpGU'><tfoot id='DUpGU'></tfoot><dl id='DUpGU'><fieldset id='DUpGU'></fieldset></dl></div>