从Python打开Excel应用程序 [英] Opening the Excel application from Python

查看:422
本文介绍了从Python打开Excel应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xlwt将其作为Python项目的一部分写入Excel文件。我还需要实际打开Excel电子表格进行显示,并将其关闭。我找到了一个函数:

I am using 'xlwt' to write into Excel files as part of my project in Python. I also need to actually open the Excel spreadsheet for display and also close it. I found a function:

import webbrowser
webbrowser.open('C:/Users/300231823/Desktop/GUI/simplenew4.xls')

这似乎打开了.xls文件。我如何关闭这个文件?

This seems to open the .xls file. How do I close the file?

我完全熟悉编程,3周前我开始使用Python。

I am completely new to programming, and I started using Python 3 weeks ago.

推荐答案

from win32com.client import Dispatch

xl = Dispatch("Excel.Application")
xl.Visible = True # otherwise excel is hidden

# newest excel does not accept forward slash in path
wb = xl.Workbooks.Open(r'C:\Users\300231823\Desktop\GUI\simplenew4.xls')
wb.Close()
xl.Quit()

win32com模块是 pywin32

The win32com module is part of pywin32.

这篇关于从Python打开Excel应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆