目录结构
01.使用bat批处理脚本打开网页
@echo off
cd /d cd /d C:\Program Files (x86)\Internet Explorer\iexplore.exe
start iexplore.exe http://newoa.by.gov.cn/oa/guard/mainframe.jsp
有些是写了没有用的 这个是main_exe.py主方法
#这个是main_exe.py
import os
#导入创建目录的包
import util.create_dir as mkd
#导入复制模板的包
import util.copy_demo as cpd
#重命名模板 复制模板的时候已经重命名了,所以不需要了。
# import util.rename_file as rf
#导入修改文档日期的包
import util.write_date as wd
#删除目录内所有文件的包
import util.dele_file as df
from xml.dom.minidom import Document
#执行bat后序文件的包
import util.exe_bat as exe
#返回目录中文件名列表的包
import util.return_dir_file_name as ref
#截图的包
import util.screen_catch as sc
#返回数字列表的包
# import util.create_number_order as cno
#时间的类
import time
#执行python文件的包 不需要用python执行python
# import util.exe_py as expy
#插入图片的包
import util.insert_image as im
#定义路径
file_name = "日常巡检手册"
seq_name = ".docx"
default_path ="C:\\Users\\human\\Desktop\\巡检截图"
current_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
origen_path = default_path+"\\"+file_name+seq_name
rename_ori = default_path+"\\"+current_time+"\\"+file_name+seq_name
rename_path = default_path+"\\"+current_time+"\\"+file_name+current_time+seq_name
final_path = default_path+"\\"+current_time+"\\"+file_name
#bat所在目录
a="D:\\Study\\myproject\\Python_auto_office\\auto_check\\bat\\"
#保存图片的目录
img_path="D:\\Study\\myproject\\Python_auto_office\\auto_check\\img\\"
ending_path=".png"
#使用关闭浏览器bat
close_blower_bat_path="D:\\Study\\myproject\\Python_auto_office\\auto_check\\util\\close_blower.bat"
screen_catch_py_path="D:\\Study\\myproject\\Python_auto_office\\auto_check\\util\\screen_catch.py"
li=ref.re_file_name_list(a)
# print(li)
#expy.exepy(screen_catch_py_path)
#创建目录
mkd.mkdir(default_path+"\\")
#复制模板
cpd.cpdemo(origen_path,rename_path)
# rf.ren(rename_ori,rename_path)
#修改文档日期
wd.info_update(rename_path,"XXXX-XX-XX", str(current_time))
#截图保存后,插入文档
for l in li:
print(a+l)
#打开浏览器
exe.cmd_exe(a+l)
#睡眠2秒
time.sleep(3)
#截图
sc.screenshot(img_path+l+ending_path)
# expy.exepy(screen_catch_py_path)
#睡眠2秒
time.sleep(1)
#关闭浏览器
exe.cmd_exe(close_blower_bat_path)
#睡眠1秒
time.sleep(1)
img_path_list=ref.re_file_name_list(img_path)
for i in img_path_list:
#插入图片 (文档,图片绝对路径,图片)
im.center_insert_img(rename_path,img_path+i,i)
#删除保存在img中目录中截图
df.del_file(img_path)
#模板是否存在
# print("06:"+str(os.path.isfile(origen_path)))
# exe.cmd_exe(a)
oa001.bat 你可以根据需要填写自已喜欢的网址,这里用IE打开百度
::这个是后序为bat的文件,其实全部都一样,只是访问的网址不一样,例如:oa001.bat
@echo off
cd /d cd /d C:\Program Files (x86)\Internet Explorer\iexplore.exe
start iexplore.exe http://baidu.com
::这个是 close_blower.bat 用来关闭IE浏览器
::taskkill /f /im SLBrowser.exe
::taskkill /f /im chrome.exe
::taskkill /f /im msedge.exe
taskkill /f /im iexplore.exe
# 这是copy_demo.py 用来复制模板到操作的目录
# coding=utf-8
import shutil
import time
origen_path = "C:\\Users\\human\\Desktop\\巡检截图\\日常巡检手册.docx"
default_path ="C:\\Users\\human\\Desktop\\巡检截图"
current_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
file_name = "日常巡检手册.docx"
final_path=default_path+current_time+"\\"+file_name
#参数源文件,目标文件
def cpdemo(origen_path,final_path):
print("cpdemo参数1: "+origen_path)
print("cpdemo参数2: "+final_path)
shutil.copyfile(origen_path,final_path)
#这是create_dir.py文件
from logging import exception
import time
import os.path
import tkinter.messagebox
defalt_path="C:\\Users\\human\\Desktop\\巡检截图\\"
def mkdir(defalt_path):
current_time=time.strftime('%Y-%m-%d',time.localtime(time.time()))
final_path=defalt_path+str(current_time)
try:
os.mkdir(final_path)
except FileExistsError:
print("文件已经存在")
tkinter.messagebox.showerror('错误',final_path+"目录已经存在")
#生成包含000-999列表
#这是create_number_order.py文件
def create_thousand_order_list(t):
l=[]
for i in range(t+1):
if i<10:
i = '00'+str(i)
elif 10<=i and i<100:
i ='0'+str(i)
else:
i=str(i)
# with open('1.txt', 'a') as f:
# f.write(i+"\n")
# print(i)
l.append(i)
return l
def create_hundred_order_list(h):
l=[]
for i in range(h+1):
if i<10:
i = "0"+str(i)
else:
i=str(i)
# print(i)
l.append(i)
return l
# li=create_thousand_order_list(67)
# print(li)
#这是dele_file.py文件
import os
import shutil
def del_file(filepath):
"""
删除某一目录下的所有文件或文件夹
:param filepath: 路径
:return:
"""
del_list = os.listdir(filepath)
for f in del_list:
file_path = os.path.join(filepath, f)
if os.path.isfile(file_path):
os.remove(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
#coding=utf-8
#这是exe_bat.py文件
addr = "D:/Study/myproject/Python_auto_office/C22/refresh_oa/refresh_oa.bat"
def cmd_exe(addr):
# cmd = 'cmd.exe d:/start.bat'
# addr = "D:/Study/myproject/Python_auto_office/C22/refresh_oa/refresh_oa.bat"
p = subprocess.Popen("cmd.exe /c" + addr, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
curline = p.stdout.readline()
while (curline != b''):
print(curline)
curline = p.stdout.readline()
p.wait()
print(p.returncode)
if __name__ == '__main__':
cmd_exe(addr)
# coding=utf-8
#这是exe_py.py文件
import time
import os
file_name="python D:\\Study\\myproject\\Python_auto_office\\C22\\refresh_oa\\receive_mail.py"
def exepy(file_name):
# 把要启动的程序放在这个方法里
# 路径要加转义符
command_line="python"+" "+file_name
os.system(command_line)
print("receive_mail.py")
# os.system("pause")
print("程序已经执行了!")
# def main():
# while True:
# time.sleep(20)
# schedule_exe()
# main()
#这是insert_image.py文件
import time
from docx import Document
from docx.shared import Inches
from docx.oxml.ns import qn
from docx.enum.text import WD_ALIGN_PARAGRAPH
#定义路径
file_name = "日常巡检手册"
seq_name = ".docx"
current_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
default_path ="C:\\Users\\human\\Desktop\\巡检截图"
rename_path=default_path+"\\"+current_time+"\\"+file_name+current_time+seq_name
image_address="D:\\Study\\myproject\\abc.png"
img_name="abc.png"
#文档地址 图片
def center_insert_img(doc, img, img_name):
"""插入图片"""
d=Document(doc)
for paragraph in d.paragraphs:
# 根据文档中的占位符定位图片插入的位置
if img_name in paragraph.text:
# 把占位符去掉
paragraph.text = paragraph.text.replace(img_name, '')
run = paragraph.add_run('')
run.add_break()
# 添加图片并指定大小
run.add_picture(img, width=Inches(6.2))
d.save(doc)
def save_img_to_doc(doc):
"""把图片保存到doc文件中的指定位置"""
# tpl_doc = doc
# res_doc = rename_path
# 打开模板文件
document = Document(doc)
# 插入图片居中
# center_insert_img(doc, img)
# 保存结果文件
document.save(doc)
def main():
"""主函数"""
img = center_insert_img(rename_path,image_address,img_name)
save_img_to_doc(rename_path)
# if __name__ == '__main__':
# main()
# coding=utf-8
#这是rename_file.py文件
import time
import os
origen_path = "C:\\Users\\human\\Desktop\\巡检截图\\日常巡检手册.docx"
default_path ="C:\\Users\\human\\Desktop\\巡检截图"
current_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
file_name = "日常巡检手册"
seq_name = ".docx"
final_path=default_path+"\\"+current_time+"\\"+file_name+seq_name
rename_path=default_path+"\\"+current_time+"\\"+file_name+current_time+seq_name
def ren(final_path,rename_path):
print("参数1:"+final_path)
print("参数2:"+rename_path)
os.rename(final_path,rename_path)
print("重命名完毕")
#这是return_dir_file_name.py文件
import os
#需要读取的路径
path_name = r'D:\Study\myproject\Python_auto_office\auto_check\bat'
def re_file_name_list(path_name):
l=[]
# item为path_name目录下的所有文件
for item in os.listdir(path=path_name):
print(item)
l.append(item)
return l
# pip install -U pillow 导入库
# 这是screen_catch.py文件
import time
from PIL import ImageGrab
def screenshot(file_name_png):
print("Ready!")
time.sleep(2)
print("Go!")
ImageGrab.grab().save(file_name_png)
# screenshot("D:\\Study\\myproject\\Python_auto_office\\auto_check\\img\\001.png")
#这是write_date.py文件
import time
import docx
from docx.shared import Inches
from docx import Document
#定义路径
file_name = "日常巡检手册"
seq_name = ".docx"
current_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
default_path ="C:\\Users\\human\\Desktop\\巡检截图"
rename_path=default_path+"\\"+current_time+"\\"+file_name+current_time+seq_name
#创建doc对像
# document = Document()
# file = Document(rename_path)
# document=file
#插入图片
#D:\Study\myproject\abc.png
# image_address="D:\\Study\\myproject\\abc.png"
#doc.add_picture("a.png")
#替换的方法
def info_update(rename_path, old_info, new_info):
# 读取段落中的所有run,找到需替换的信息进行替换
document=Document(rename_path)
for para in document.paragraphs:
for run in para.runs:
run.text = run.text.replace(old_info, new_info) # 替换信息,第三位为最多替换几次
#插入图片run.add_picture('a.png') # 插入图像,可以是内存中的图像,width=Inches(1.0)指定宽度。
# 读取表格中的所有单元格,找到需替换的信息进行替换
for table in document.tables:
for row in table.rows:
for cell in row.cells:
cell.text = cell.text.replace(old_info, new_info) # 替换信息old_info, new_info
document.save(rename_path)
# info_update(document, "XXXX-XX-XX", str(current_time))
# document.save(rename_path)