由于pyautogui无法在电脑软件上进行粘贴操作,故通过向日葵远程桌面进行粘贴操作
发送任务端
# - * - coding : utf-8 - * -
"""====================================================================================================================
function : PDF转图片自动化
===================================================================================================================="""
import os
import time
import shutil
import pyautogui
from loguru import logger
from utils.db_conf import MysqlConf
class PdfToPic:
def __init__(self):
self.db = MysqlConf()
def main(self):
pdf_files = os.listdir('产品样册') # PDF存放目录
for index, pdf_file in enumerate(pdf_files):
if index < 0:
continue
logger.info(f'{index}, {pdf_file}')
self.pdf_to_pic(pdf_file)
def pdf_to_pic(self, pdf_file):
# 移动到hh文件夹
self.move_in(pdf_file)
# 告诉另一机它可以工作了并监听对方是否结束
self.update_signal_and_listening()
# 将图片移至pdf_pic文件夹
self.move_to_pdf_pic(pdf_file)
# 删除PDF
self.move_out(pdf_file)
def move_in(self, pdf_name):
shutil.copy(f'产品样册/{pdf_name}', f'hh/{pdf_name}')
def move_out(self, pdf_name):
os.remove(f'hh/{pdf_name}')
def move_to_pdf_pic(self, pdf_name):
sku = pdf_name[:-4]
while True:
try:
shutil.move(f'hh/{sku}/{sku}_00.jpg', f'pdf_pic/{sku}_3.jpg')
break
except FileNotFoundError:
continue
os.rmdir(f'hh/{sku}')
def update_signal_and_listening(self):
table = 'signal_pdf_to_pic'
condition = 'platform = "SMC"'
set = 'signal_check = 0'
self.db.update(table, set, condition)
# 监听
while True:
time.sleep(1)
signal = self.db.signal_check()
if signal == 1:
break
if __name__ == '__main__':
time.sleep(10)
p = PdfToPic()
p.main()
处理端
# - * - coding : utf-8 - * -
"""====================================================================================================================
function : SMC商品PDF转图片自动化
pyautogui的键盘功能不能在软件上实现! -> 通过远程桌面间接实现!
两机联动处理: 本机得到图片(图片存储位置在另一机上),另一机随时待命,出现图片即处理
===================================================================================================================="""
import time
import pyautogui
import pyperclip
from utils.db_conf import MysqlConf
class PdfToPic:
def __init__(self):
self.db = MysqlConf()
pyperclip.copy('1-5')
def pdf_to_pic(self):
# 打开文件夹
self.click('open_file.png', 2)
# 点击文件
self.click('open_pdf.png')
# 打开文件
self.click('open.png')
# 点击PDF转图片
self.click('pdf_to_pic.png')
# 点击合成长图
self.click('merge_long_pic.png')
# 点击页码选择
self.click('page_choose.png')
# 点击页码选择框
self.click('page_choose_column.png')
# 输入页码
self.input_page()
# # 输出尺寸
# self.click('2times.png')
# self.click('1times.png')
# 输出
self.click('output.png')
time.sleep(3)
# 输出成功
self.click('success.png')
# 叉掉选择页面
self.click('delete.png')
# 关闭文件
self.click('close_file.png')
def click(self, pic, times=1):
"""
times: 点击次数
"""
while True:
l = pyautogui.locateCenterOnScreen(pic, confidence=0.7)
if l:
break
for i in range(times):
pyautogui.click(l)
time.sleep(0.5)
def input_page(self):
time.sleep(0.5)
pyautogui.hotkey('ctrl', 'v')
def update_signal(self):
table = 'signal_pdf_to_pic'
condition = 'platform = "SMC"'
set = 'signal_check = 1'
self.db.update(table, set, condition)
def main(self):
while True:
signal = self.db.signal_check()
if signal == 0:
self.pdf_to_pic()
self.update_signal()
time.sleep(1)
if __name__ == '__main__':
p = PdfToPic()
p.main()
# p.pdf_to_pic()