用邮件的方式 仓库每日定时汇报 入库件数。
import os
import sys
import time
import xlrd
import xlwt
import win32gui, win32api
import win32con
from pynput import mouse, keyboard
import csv
import os
import pandas as pd
import time
import smtplib
import schedule
import time
from email.mime.text import MIMEText
from email.header import Header
import pandas as pd
mouse_control = mouse.Controller()
keyboard_control = keyboard.Controller()
t = '入库件数'
def click_and_wait(x, y):
time.sleep(1)
mouse_control.position = (x, y)
time.sleep(0.5)
mouse_control.click(mouse.Button.left, 1)
time.sleep(1)
def click_and_send_text(x, y, text):
time.sleep(0.5)
mouse_control.position = (x, y)
time.sleep(0.5)
mouse_control.click(mouse.Button.left, 1)
time.sleep(0.5)
keyboard_control.type(text)
time.sleep(0.5)
def close_erp_tabs():
mouse_control.position = (57, 113)
mouse_control.click(mouse.Button.right, 1)
time.sleep(0.5)
mouse_control.position = (90, 190)
mouse_control.click(mouse.Button.left)
time.sleep(0.5)
def create_product(filename):
# 点开统计
click_and_wait(822, 46)
# 点绩效统计
click_and_wait(825, 255)
# 点入库员绩效
click_and_wait(1003, 673)
# 箭头
click_and_wait(838, 152)
# 采购入库
click_and_wait(720, 237)
# 确定
click_and_wait(880, 331)
# 搜索
click_and_wait(1383, 155)
# 导出
# 右键
mouse_control.position = (394, 200) # 鼠标放在的位置
mouse_control.click(mouse.Button.right, 1) # 全选货品 # 鼠标右键单击一次
click_and_wait(427, 265) # 左键
#click_and_wait(894, 340)
# 右键选中
click_and_send_text(830, 338,t) # 输入货品编码
keyboard_control.press(keyboard.Key.enter)
time.sleep(1)
#否
click_and_wait(1079, 609)
#关闭erp
click_and_wait(1900, 16)
click_and_wait(940, 608)
def open_erp():
wdt_handle = win32gui.FindWindow('CoolWindow', '旺店通ERP旗舰版')
if not wdt_handle: # 旺店通ERP旗舰版.exe - 快捷方式
path1 = os.path.dirname(os.path.abspath(__file__))
ph_ls = os.listdir(os.path.join(path1))
erp_path = None
for ph_ls_ in ph_ls:
if '旺店通ERP旗舰版' in ph_ls_:
erp_path = os.path.join(path1, ph_ls_)
if not erp_path:
print('没有找到erp快捷方式')
return None
# os.startfile('D:\\旺店通ERP旗舰版\\旺店通ERP旗舰版.exe')
os.startfile(erp_path)
time.sleep(10)
wdt_handle = win32gui.FindWindow('CoolWindow', '旺店通ERP旗舰版')
if not wdt_handle:
return None
win32gui.SetWindowPos(wdt_handle, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
time.sleep(1)
win32gui.SetWindowPos(wdt_handle, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
time.sleep(1)
# 关闭弹出的对话框
handle = win32gui.FindWindow('#32770 (Dialog)', '旺店通管理系统')
if handle != 0:
keyboard_control.press(keyboard.Key.esc)
time.sleep(1)
# 关闭弹出的对话框
handle = win32gui.FindWindow('#32770', '旺店通ERP旗舰版')
if handle != 0:
keyboard_control.press(keyboard.Key.esc)
time.sleep(1)
ddhh = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(ddhh)
if title == '修改订单':
# 点“取消”关闭订单换货
mouse_control.position = (180, 47)
mouse_control.click(mouse.Button.left, 1)
time.sleep(0.5)
#close_erp_tabs()
return wdt_handle
if __name__ == '__main__':
x = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
y = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
#info = input('运行前先关闭正在运行的erp,执行回车后就不要再动鼠标' + '\n' + ':')
time.sleep(3)
h = open_erp()
if not h:
print('没有打开ERP')
sys.exit(1)
path = os.path.dirname(os.path.abspath(__file__))
sku_list = []
#if str(info) != '1':
close_erp_tabs()
create_product(os.path)
print('文件已导出' + ' : ' + t + '.csv')
account = '****@qq.com'
password = '密码' # 失效时再次更改! 密码为授权码
receiver = '****@qq.com'
index = 0
# index的目的是让程序只运行两次就结束
quan = pd.read_csv('C:\\Users\\Administrator\\Desktop\\新建文件夹\\入库件数.csv')
def stt():
data = pd.read_csv('今日入库件数.csv', encoding='utf-8')
with open('news_data.txt','a+', encoding='utf-8') as f:
for line in data.values:
f.write((str(line[0])+'\t'+str(line[1])+'\n'))
def send_email(movie_list):
global account, password, receiver
mailhost = 'smtp.qq.com'
qqmail = smtplib.SMTP_SSL()
qqmail.connect(mailhost, 465)
qqmail.login(account, password)
content = (movie_list)
print(content)
message = MIMEText(content, 'plain', 'utf-8')
subject = '今日入库件数:'
message['Subject'] = Header(subject, 'utf-8')
try:
qqmail.sendmail(account, receiver, message.as_string())
print('邮件发送成功')
except:
print('邮件发送失败')
qqmail.quit()
def job():
global index
print('开始任务')
#movie_list = t
send_email(quan.to_string())
#print(quan)
print('任务完成')
index += 1
schedule.every().day.at("18:30").do(job)
while index != 2:
#这里会当index == 2的时候程序结束
schedule.run_pending()
time.sleep(1)