最近被已畢業(yè)的師兄拉過(guò)去給一次會(huì)議當(dāng)注冊(cè)人員赘方,要挨個(gè)給參會(huì)人員制作工牌榜苫,就是將下面的表單數(shù)據(jù)里面的人名以及其拼音铝宵,按照給的ppt模版手工挨個(gè)復(fù)制粘貼過(guò)去碴犬。
雖然參會(huì)人員沒(méi)有成千上萬(wàn)人絮宁,我們幾個(gè)機(jī)動(dòng)注冊(cè)人員手動(dòng)干活的話也就拼音那塊要手動(dòng)敲,但愛(ài)偷懶的我還是想萬(wàn)物皆可編程解決
服协,于是很快就想出了以下解決方案:
- 復(fù)制姓名一列到
txt
文檔中绍昂,利用Python
按行讀取文檔內(nèi)容轉(zhuǎn)換成list
,安裝導(dǎo)入xpinyin
模塊偿荷,獲取每個(gè)姓名對(duì)應(yīng)的拼音窘游,將其按行寫(xiě)入到一個(gè)新的txt
文檔。- 利用
Python
的subprocess.Popen
打開(kāi)指定路徑的ppt
文件跳纳,并使用SendKeys
模塊以及clipboard
模塊對(duì)這項(xiàng)純粹是復(fù)制粘貼的重復(fù)手工勞動(dòng)力活進(jìn)行自動(dòng)化操作
1. 文字轉(zhuǎn)拼音
用pip install
安裝好官網(wǎng)的xpinyin
然后列出代碼忍饰,慢慢講解:
# -*- coding:utf-8 -*-
import requests
import urllib2
import re
import sys
import io
from selenium import webdriver
import time
import math
import os
from pywinauto import application
import SendKeys
import pymouse,pykeyboard,os,sys
from pymouse import *
from pykeyboard import PyKeyboard
import win32api
import win32con
import pyautogui
import subprocess
import win32clipboard as clipboard
import chardet
from xpinyin import Pinyin
p = Pinyin()
def pinyinTransfer(name_list):
pinyin_array=[]
for key in range(0,len(name_list)):
a=name_list[key].decode('utf-8')
b=p.get_pinyin(a, ' ')
# print b
c=b.split(' ')
d=[]
for num in range(0,len(c)):
temp0=c[num].encode('utf-8')
if (num>1):
pass
else:
temp0=temp0.capitalize()
d.append(temp0)
# print d
num=len(d)
pinyin=''.join(d[1::])+' '+d[0]
# print pinyin
pinyin_array.append(pinyin)
return pinyin_array
# print pinyinTransfer(['黃健輝','陳晨'])
temp_path='D:\\蒙皮會(huì)議\\'
keyword='Namelist'
temp=temp_path+keyword+".txt"
pinyin_list = temp_path + keyword + "_pinyin.txt"
print pinyin_list
fp= open(unicode(temp, 'utf-8'),'r') #設(shè)置文件對(duì)象
lines = fp.readlines()
lists = []#直接用一個(gè)數(shù)組存起來(lái)就好了
count=0
for line in lines:
#print line
temp=line.split()
# print temp
count=count+1
if(count==1):
lists.append(temp[0].decode("utf-8-sig").encode('utf-8'))
else:
lists.append(temp[0])
fp.close()
pinyin_array=pinyinTransfer(lists)
fp = open(unicode(pinyin_list, 'utf-8'), 'w') # 文件名不亂碼
fp.write('\n'.join(pinyin_array))
fp.close()
注意事項(xiàng)講解:
fp= open(unicode(temp, 'utf-8'),'r') #設(shè)置文件對(duì)象
當(dāng)讀取的文件路徑有中文字符時(shí),為了保證不出錯(cuò)寺庄,我們把已知編碼為
utf-8
編碼的路徑轉(zhuǎn)化為unicode
通用二進(jìn)制編碼艾蓝。(可以用chardet.detect()
來(lái)檢測(cè)編碼)
lines = fp.readlines() lists = []#直接用一個(gè)數(shù)組存起來(lái)就好了 count=0 for line in lines: #print line temp=line.split() # print temp count=count+1 if(count==1): lists.append(temp[0].decode("utf-8-sig").encode('utf-8')) else: lists.append(temp[0]) fp.close() pinyin_array=pinyinTransfer(lists)
txt文檔每行只有一個(gè)名字,所以我們可以按行讀取斗塘,然后用
split
函數(shù)將\n
與姓名分開(kāi)赢织,第一個(gè)就是姓名。lists.append(temp[0].decode("utf-8-sig").encode('utf-8'))
這句代碼的寫(xiě)入是因?yàn)樽x取的時(shí)候發(fā)現(xiàn)開(kāi)頭的二進(jìn)制文件多了一部分馍盟,網(wǎng)上查找后發(fā)現(xiàn)這是因?yàn)楸4娴氖?code>utf-8-sig格式導(dǎo)致于置,我們僅對(duì)第一行進(jìn)行處理即可。
pinyinTransfer(lists)
中贞岭,一開(kāi)始把姓名進(jìn)行解碼.decode('utf-8')
八毯,為了實(shí)現(xiàn)周杰倫-Jielun Zhou
的拼音效果,我們用p.get_pinyin(a, ' ')
代碼使得獲得的拼音之間以空格符分割開(kāi)瞄桨,然后用b.split(' ')
將其分為幾部分话速。然后我們將前2個(gè)拼音的首字母進(jìn)行大寫(xiě),然后按照想要的順序編排pinyin=''.join(d[1::])+' '+d[0]
芯侥。
2. Python操控ppt
這部分內(nèi)容是通過(guò)模擬人工方法泊交,并非利用ppt的宏,編寫(xiě)執(zhí)行vbs來(lái)達(dá)到效果的。
和人工方法一樣活合,先打開(kāi)指定的ppt文件,然后點(diǎn)擊要復(fù)制粘貼的文本框進(jìn)行替換物赶,再按下鍵盤(pán)的↓切換到下一頁(yè)白指,反復(fù)操作即可。先放出代碼:
# -*- coding:utf-8 -*-
import requests
import urllib2
import re
import sys
import io
from selenium import webdriver
import time
import math
import os
from pywinauto import application
import SendKeys
import pymouse,pykeyboard,os,sys
from pymouse import *
from pykeyboard import PyKeyboard
import win32api
import win32con
import pyautogui
import subprocess
import win32clipboard as clipboard
import chardet
import subprocess
def ppt_more():
mouse = PyMouse()
try:
time.sleep(1)
exeName = 'D:\Software\Office_2013\Office15\POWERPNT.EXE'
fileName = r'D:\conference\Card.pptx'
file = exeName + " " + fileName
subprocess.Popen(file)
print(fileName+'打開(kāi)成功酵紫!')
time.sleep(10)
mouse.click(80, 250)
time.sleep(0.1)
SendKeys.SendKeys('^a')
time.sleep(0.1)
SendKeys.SendKeys('^c')
num=203
for k in range(0,num):
SendKeys.SendKeys('^v')
time.sleep(0.1)
time.sleep(1)
SendKeys.SendKeys('^s')
time.sleep(10)
# os.system(r'taskkill /F /IM POWERPNT.EXE')
mouse.click(1904, 10)
print (fileName+'關(guān)閉成功告嘲!')
except:
pass
# fp1.close()
def clipfunc(word):
clipboard.OpenClipboard()
clipboard.EmptyClipboard()
clipboard.SetClipboardData(win32con.CF_TEXT, word.decode('utf-8').encode('gbk'))
clipboard.CloseClipboard()
print word
def anjianFunc():
time.sleep(0.1)
SendKeys.SendKeys('^v')
# SendKeys.SendKeys('+{F10}')
# time.sleep(0.1)
# SendKeys.SendKeys('{DOWN}')
# time.sleep(0.1)
# SendKeys.SendKeys('{DOWN}')
# time.sleep(0.1)
# SendKeys.SendKeys('{DOWN}')
# time.sleep(0.1)
# SendKeys.SendKeys('{DOWN}')
# time.sleep(0.1)
# SendKeys.SendKeys('{Right}')
# time.sleep(0.1)
# SendKeys.SendKeys('{Right}')
# time.sleep(0.1)
# SendKeys.SendKeys('{ENTER}')
def ppt_card_name(namelists,pinyinlists):
mouse = PyMouse()
try:
time.sleep(1)
exeName = 'D:\Software\Office_2013\Office15\POWERPNT.EXE'
fileName = r'D:\conference\Card_more.pptx'
file = exeName + " " + fileName
subprocess.Popen(file)
print(fileName + '打開(kāi)成功!')
time.sleep(3)
for num in range(0,len(namelists)):
time.sleep(0.1)
mouse.click(1054, 471)
time.sleep(0.1)
SendKeys.SendKeys('^a')
time.sleep(0.1)
clipfunc(namelists[num])
time.sleep(0.1)
anjianFunc()
time.sleep(0.1)
mouse.click(1054, 571)
time.sleep(0.1)
SendKeys.SendKeys('^a')
time.sleep(0.1)
clipfunc(pinyinlists[num])
time.sleep(0.1)
anjianFunc()
time.sleep(0.1)
mouse.click(820, 930)
time.sleep(0.1)
if (num==len(namelists)):
pass
else:
SendKeys.SendKeys('{DOWN}')
except:
pass
# ppt_more()
temp=r'D:\conference\Namelist.txt'
fp= open(unicode(temp, 'utf-8'),'r') #設(shè)置文件對(duì)象
lines = fp.readlines()
namelists = []#直接用一個(gè)數(shù)組存起來(lái)就好了
for line in lines:
temp=line.split()
namelists.append(temp[0].decode("utf-8-sig").encode('utf-8'))
fp.close()
temp=r'D:\conference\Namelist_pinyin.txt'
fp= open(unicode(temp, 'utf-8'),'r') #設(shè)置文件對(duì)象
lines = fp.readlines()
pinyinlists = []#直接用一個(gè)數(shù)組存起來(lái)就好了
for line in lines:
temp=line.replace('\n','')
pinyinlists.append(temp.decode("utf-8-sig").encode('utf-8'))
fp.close()
ppt_card_name(namelists,pinyinlists)
ppt_more()
這個(gè)函數(shù)是將ppt中一頁(yè)幻燈片模版復(fù)制粘貼為你想要的頁(yè)數(shù)奖地,比如參會(huì)人員有N個(gè)橄唬,那么就要有N頁(yè)ppt參會(huì)工牌。subprocess.Popen
函數(shù)可以用來(lái)打開(kāi)特定文件参歹,exeName
和fileName
分別是程序的exe絕對(duì)路徑和要打開(kāi)文件的絕對(duì)路徑仰楚,這個(gè)不可以包含中文。代碼中的time.sleep()
不能去掉犬庇,否則可能會(huì)引起時(shí)序紊亂僧界。SendKeys.SendKeys
能很好地模擬鍵盤(pán)輸入。clipfunc
是模擬復(fù)制字符串到剪切板臭挽,這里最好對(duì)待復(fù)制的中文字符進(jìn)行處理.decode('utf-8').encode('gbk')
捂襟。anjianFunc()
中如果ppt是默認(rèn)無(wú)格式粘貼的話,就可以直接用代碼欢峰,如果粘貼的時(shí)候要選擇格式葬荷,就要根據(jù)情況,像被注釋掉的代碼樣進(jìn)行選擇粘貼操作纽帖。ppt_card_name()
這個(gè)函數(shù)就是模擬人工打開(kāi)ppt宠漩。在程序運(yùn)行前mouse.click(1054, 471)
的參數(shù)需要根據(jù)自己電腦確定,我電腦是1920*1080分辨率抛计,這個(gè)屏幕坐標(biāo)是在點(diǎn)擊ppt右上方中的□最大化測(cè)量出來(lái)的哄孤。
最后上一張動(dòng)態(tài)圖:
喜歡的朋友可以收藏哦,代碼可以直接用吹截!
個(gè)人名片
姓名 畢業(yè)院校 專業(yè) 專業(yè)技能 小輝 電子科技大學(xué)(本科+碩士) 電磁場(chǎng)與微波技術(shù) HFSS瘦陈,CST,F(xiàn)EKO波俄,AutoCAD晨逝,MATLAB(GUI),Python