0
点赞
收藏
分享

微信扫一扫

分享一个自动化截图程序

悲催博士僧 2022-03-11 阅读 73

-- coding: utf-8 --

from pywinauto.application import Application
from docx import Document
import win32api
import win32gui
import time
from win32com import client
from PIL import ImageGrab, Image, ImageDraw, ImageFont
import os
from win32com.client import gencache
from win32com.client import constants, gencache

createPdf wordToPdf :word自动转为pdf保存,方便截图

def createPdf(wordPath, pdfPath):
word = gencache.EnsureDispatch(‘Word.Application’)
doc = word.Documents.Open(wordPath, ReadOnly=1)
doc.ExportAsFixedFormat(pdfPath,
constants.wdExportFormatPDF,
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
word.Quit(constants.wdDoNotSaveChanges)

def wordToPdf():
c_path = os.getcwd()
path =os.path.join(c_path,‘word’)
filename_list = os.listdir(path)
wordname_list = [filename for filename in filename_list
if filename.endswith((".doc", “.docx”))]
for wordname in wordname_list:
pdfname = os.path.splitext(wordname)[0] + ‘.pdf’
if pdfname in filename_list:
continue
wordpath = os.path.join(path, wordname)
pdfpath = os.path.join(path, pdfname)
createPdf(wordpath,pdfpath)
#CaptureImage pillow截图 program_path为图王pdf阅读器exe地址
def CaptureImage(file_path,file):
c_path = os.getcwd()
program_path=r’C:\Program Files\2345Soft\2345Pic\2345PdfReader.exe’
app = Application().start(r’{} “{}”’.format(program_path, file_path)) #自动化打开pdf文档
time.sleep(2)
win = app.top_window()
win.type_keys(’^Q’)
im = ImageGrab.grab((318,117,1075,720))
width, height = im.size
im = im.resize((4724, 4252), Image.ANTIALIAS) #提高分辨率
save_path = os.path.join(c_path,‘output’,file)
im.save(’%s.png’%save_path)
win.close()
def main():
c_path = os.getcwd()
word_path=os.path.join(c_path,‘word’)
wordToPdf()
for file in os.listdir(word_path):
if not ‘$’ in str(file) and ‘.pdf’ in str(file):
file_path = os.path.join(word_path,file)
CaptureImage(file_path,file)
else:
pass
page_path=os.path.join(c_path,‘output’)
pages=len([name for name in os.listdir(page_path)])
for file in os.listdir(page_path): #这个循环自动打开图片添加页码
im = Image.open(os.path.join(page_path,file))
font = ImageFont.truetype(font=c_path+’//simsun.ttc’, size=85)
list1 = file.split(’.’)
list2= list1[0].split(’_’)
text = ‘第{}页,共{}页’.format(int(list2[5])-1,pages-1)
draw=ImageDraw.Draw(im)
draw.text(xy=(4100,4100), text=text, fill=(0, 0, 0), font=font)
save_path = os.path.join(c_path,‘output’,file)
im.save(save_path)
if name == main():
main()

举报

相关推荐

0 条评论