从Python自动化Excel [英] Automation Excel from Python

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

问题描述

在我公司,我们在开发和生产环境中使用Linux。但是我们有一台运行Windows和Excel的机器,因为我们使用第三方应用程序excel addin来获取机器的金融市场数据。该加载项提供了一些功能(就像Excel函数)一样,用于将这些数据导入本地机器,然后发送回MySql数据库。我们还开发了一些VBA脚本来实现自动化的任务,但仍然不能满足结果。
我正在考虑使用Python来完成所有这些工作,但是在跳进之前,我需要找到一个可以执行的python包。



  1. 如果我需要打开Excel,我需要自动完成执行该任务的任务,然后使用python来处理Excel(及其加载项)脚本每天或在一天的特定时刻(市场数据需要提供特定的时间)

感谢您的建议

解决方案

你需要Python Win32扩展 - http://sourceforge.net/projects/pywin32/



然后你可以使用COM。


$ b $来自win32com.client import的b

 调度
excel = Dispatch('Excel.Application')
wb = excel.Workbooks.Open(r'c:\\
ws = wb.Sheets('我的工作表')
#做其他的东西,就像VBA
wb.Close()
excel.Quit()

您可以将脚本放在Windows任务计划程序上,以运行您需要的时间。


In my company, we use Linux in development and production environment. But we have a machine running Windows and Excel because we use a third party application excel addin to get financial market data to the machine. The add-in provides some functions (just like Excel function) for getting these datas into the local machine and then sending back to a MySql Database. We've also developed some VBA script to automation the task but still not satisfy with the result. I'm considering using Python to do all these jobs, but before jumping in, i need to find a python package that can do

  1. Use python to manipulate Excel (with its add-ins) and use its functions without opening Excel?
  2. If I need to open Excel, I need to automate that task of executing the script every day, or in specific moment of the day (the market data need to be feed a specific time)

Thanks for suggestion

解决方案

You'll need the Python Win32 extensions - http://sourceforge.net/projects/pywin32/

Then you can use COM.

from win32com.client import Dispatch
excel = Dispatch('Excel.Application')
wb = excel.Workbooks.Open(r'c:\path\to\file.xlsx')
ws = wb.Sheets('My Sheet')
# do other stuff, just like VBA
wb.Close()
excel.Quit()

You can put your script on Windows Task Scheduler to run for the times you need.

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

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