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

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

问题描述

我使用'xlwt写入到Excel文件作为我在Python项目的一部分。我还需要实际打开显示Excel US preadsheet,也将其关闭。我发现了一个功能:

 进口web浏览器
webbrowser.open('C:/Users/300231823/Desktop/GUI/simplenew4.xls')

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

我完全新的节目,我开始使用Python 3个星期前。


解决方案

 从win32com.client进口调度XL =调度(Excel.Application)
xl.Visible = TRUE#否则Excel是隐藏#最新Excel不接受转发路径斜杠
WB = xl.Workbooks.Open(R'C:\\用户\\ 300231823 \\桌面\\ GUI \\ simplenew4.xls')
wb.Close()
xl.Quit()

该win32com模块的一部分 pywin32

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')

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

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()

The win32com module is part of pywin32.

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

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