网站首页 > 技术文章 正文
EXCEL利用VBA编程可以实现强大的功能,我之前做了一些基于VBA的程序,实现数据采集、生成图片报表等功能,将这些程序放到服务器定时执行,原考虑用VBA ontime 函数实现,但是有10几个EXCEL文件和自带程序,运行占内存且问题估计会比较多。
考虑用Python制作一个定时器,到指定时间打开excel,执行excel宏,然后再关闭excel
经过查阅资料,最终成功实现程序如下:
1、定时部分:
def dingshi():
from datetime import datetime
ttime1=28800 #早上8:00,转化为秒
ttime2=61200 #下午5:00,转化为秒
print( "定时程序已启动,启动时间:"+datetime.now().strftime( '%Y-%m-%d %H:%M:%S'))
while True:
hh = datetime.now().hour
mm = datetime.now().minute
ss = datetime.now().second
zm = hh*3600+mm*60+ss #当前时间,转化为秒
time.sleep(1) #延时间隔1秒
if zm == ttime1 or zm == ttime2: #此处设置每天定时的时
jivy()
2、启动EXCEL、运行宏、关闭EXCEL
def jivy():
xl = win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="E:\\服务器\\fengchuang7.xlsm", ReadOnly=1)
try:
xl.Application.Run("fengchuang7.xlsm!模块2.moju")
xl.Workbooks(1).Close(SaveChanges=0)
xl.Application.Quit()
xl = 0
print("模具状态更新程序已执行")
except:
xl.Workbooks(1).Close(SaveChanges=0)
xl.Application.Quit()
xl = 0
全部代码
import win32com.client
import time
def jivy():
xl = win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="E:\\服务器\\jivy7.xlsm", ReadOnly=1)
try:
xl.Application.Run("jivy.xlsm!模块2.moju")
xl.Workbooks(1).Close(SaveChanges=0)
xl.Application.Quit()
xl = 0
print("程序已执行")
except:
xl.Workbooks(1).Close(SaveChanges=0)
xl.Application.Quit()
xl = 0
def dingshi():
from datetime import datetime
ttime1=28800 #早上8:00,转化为秒
ttime2=61200 #下午5:00,转化为秒
print( "定时程序已启动,启动时间:"+datetime.now().strftime( '%Y-%m-%d %H:%M:%S'))
while True:
hh = datetime.now().hour
mm = datetime.now().minute
ss = datetime.now().second
zm = hh*3600+mm*60+ss #当前时间,转化为秒
time.sleep(1) #延时间隔1秒
if zm == ttime1 or zm == ttime2: #此处设置每天定时的时
jivy()
dingshi()
说明:需要 win32com.client 模块 pip install pypiwin32
猜你喜欢
- 2024-12-26 在Python中使用Asyncio系统(3-6)?优雅地开启和关闭协程
- 2024-12-26 树莓派开机启动脚本
- 2024-12-26 Python 30 天探索:文件读取的奥秘之旅
- 2024-12-26 Python 面向对象编程:开启高效编程之旅
- 2024-12-26 PHP/Python如何开启自带的静态文件 web 服务器
- 2024-12-26 一篇文章搞定Django项目上线与部署方法
- 2024-12-26 青少年之Python编程课程安排
- 2024-12-26 python:快速启动-android稳定性测试
- 2024-12-26 Python 实现函数的异步启动和异步结束
- 2024-12-26 Python 网络编程大揭秘:Socket 开启互联互通新世界
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 256℃python笔记:for循环嵌套。end=""的作用,图形打印
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)