python中自動化辦公 【筆記】

00讀取csv文件

import csv
def readCsv(path):infolist = []with open (path,"r") as f:allFileInfo = csv.reader(f)print(allFileInfo)for row in allFileInfo:infolist.append(row)return infolistpath =r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\2、讀寫csv文件\000001.csv"
info = readCsv(path)
# [[],[],[]]

01寫csv文件

import csvdef writeCsv(path,data):with open(path,"w")as f:write = csv.writer(f)for rowData in data:print("*********")write.writerow(rowData)path =r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\2、讀寫csv文件\000003.csv"
writeCsv(path, [[1,2,3],[4,5,6],[7,8,9]])

02讀取pdf文件

import sys
import importlib
importlib.reload(sys)from pdfminer.pdfparser import PDFParser, PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LTTextBoxHorizontal, LAParams
from pdfminer.pdfinterp import PDFTextExtractionNotAlloweddef readPDF(path,topath):#以二進制打開pdf文件f = open(path,"rb")#創建一個PDF文檔分析器parse = PDFParser(f)#創建PDF文檔pdfFile = PDFDocument()#鏈接 分析器與文檔對象 互相連接parse.set_document(pdfFile)pdfFile.set_parser(parse)#提供初始化密碼pdfFile.initialize()#檢測文檔是否提供txt轉換if not pdfFile.is_extractable:raise PDFTextExtractionNotAllowedelse:#解析數據#數據管理器manager = PDFResourceManager()#創建一個PDF設備的對象laparams= LAParams()device = PDFPageAggregator(manager,laparams=laparams)#解釋器對象interpreter = PDFPageInterpreter(manager,device)#開始循環處理,每次處理一頁for page in pdfFile.get_pages():interpreter.process_page(page)layout =device.get_result()for x in layout:if (isinstance(x,LTTextBoxHorizontal)):with open(topath,"a")as f:str = x.get_text()# print(str)f.write(str+"\n")topath=r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\a.txt"
path =r"D:\xiazaipan\第1章  Python語言基礎\16、py2與py3的區別和測試\0-作業\文件的封裝\sunck.pdf"
readPDF(path,topath)

03播放音樂

#pip install pygameimport time
import pygame#音樂路徑
filePath = r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\7、播放音樂\res\0.mp3"# 初始化
pygame.mixer.init()#加載音樂
track = pygame.mixer.music.load(filePath)#播放
pygame.mixer.music.play()#
time.sleep(5)
pygame.mixer.music.pause()#暫停
#停止
pygame.mixer.music.stop()

04修改背景圖片

# win鍵加R   ->regedit ->HKEY_CURRENT_USER->
#Control panel->Desktop->import win32api
import win32con
import win32guidef setWallPaper(path):#打開注冊表reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)#2拉伸 0居中 6適應 10填充win32api.RegSetValueEx(reg_key,"WallpaperStyle",0,win32con.REG_SZ,"6")## win32api.RegSetValueEx(reg_key,)# win32api.RegSetValueEx(reg_key,"WallPaper" )# win32con.SPIF_SENDWININICHANGE立即生效win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,path,win32con.SPIF_SENDWININICHANGE)# 圖片地址
setWallPaper(r"")

05整蠱程序

import time
import pygame
import win32api
import win32con
import win32gui
import threadingdef go():pygame.mixer.init()while 1:for i in range(5):filePath =\r"H:\QIANfeng code\17自動化辦公鼠標鍵盤模擬\res"+"\\"+str(i)+".mp3"track = pygame.mixer.music.load(filePath)pygame.mixer.music.play()time.sleep(10)pygame.mixer.music.stop()def setWallPaper(path):reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)win32api.RegSetValueEx(reg_key,"WallpaperStyle",0,win32con.REG_SZ,"6")win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,path,win32con.SPIF_SENDWININICHANGE)
#線程
th = threading.Thread(target = go ,name ="LoopThread")
th.start()
while True:go()for i in range(9):filePath = r"H:\QIANfeng code\17自動化辦公鼠標鍵盤模擬\res1"+"\\"+str(i)+".jpeg"print(filePath)setWallPaper(filePath)time.sleep(5)

06鍵盤模擬

import win32con
import win32api
import time'''
win32api.keybd_event(91,0,0,0)
time.sleep(0.1)
win32api.keybd_event(91,0,win32con.KEYEVENTF_KEYUP,0)
'''
while 1 :win32api.keybd_event(91,0,0,0)time.sleep(0.1)win32api.keybd_event(77,0,0,0)time.sleep(0.1)win32api.keybd_event(77,0,win32con.KEYEVENTF_KEYUP,0)win32api.keybd_event(91, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(3)

07語音控制游戲

08鼠標模擬

import win32con
import win32api
import timewin32api.SetCursorPos([30,40])
time.sleep(0.1)
#鼠標左鍵按下
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0)
#鼠標左鍵抬起 
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0)win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0)

09讀取doc與docx文件

import win32com
import win32com.clientdef readWordFile(path):#調用系統word功能,可以處理doc和 docx兩種文件mw = win32com.client.Dispatch("Word.Application")#打開文件doc = mw.Documents.Open(path)for paragraph in doc.Paragraphs:line = paragraph.Range.Textprint(line)#關閉文件doc.Close()#退出文件mw.Quit()path = r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\4、word自動化辦公\sunck.doc"
readWordFile(path)

10讀取doc與docx文件并寫入其他文件

import win32com
import win32com.clientdef readWordFile(path):mw = win32com.client.Dispatch("Word.Application")doc = mw.Documents.Open(path)#將word的數據保存到另一個文件doc.SaveAs(toPath,2)#2表示txt文件doc.Close()mw.Quit()toPath = r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\4、word自動化辦公\1.txt"

11創建word文件

import win32com
import win32com.client
import osdef makeWordFile(path,name):word = win32com.client.Dispatch("Word.Application")#創建文檔doc = word.Documents.Add()#文檔可見word.Visible = True#寫內容#從頭開始寫r = doc.Range(0,0)r.InsertAfter("親愛的"+name+"\n")r.InsertAfter("想你")#存儲文件doc.SaveAs(path)#關閉文件doc.Close()#退出文件# word.Quit()names = ["zhangsan","lisi","wangwu"]
for name in names:path = os.path.join(os.getcwd(),name)makeWordFile(path,name)path = r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\4、word自動化辦公"

12讀取xlsx文件

#xlsx xls
#openpyxl->xlsxfrom openpyxl.reader.excel import load_workbookdef readXlsxFile(path):file= load_workbook(filename = path)print(file.get_sheet_names())sheets = file.get_sheet_names()#拿出一個表格sheet = file.get_sheet_by_name(sheets[0])#最大行數print(sheet.max_row)#最大列數print(sheet.max_column)#表名print(sheet.title)for lineNum in range(1,sheet.max_row+1):print(lineNum)lineList = []for columnNum in range(1,sheet.max_column):#拿數據value = sheet.cell(row=lineNum,column = columnNum).value# if value!=None:lineList.append(value)print(lineList)path= r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\5、excel自動化辦公\1.xlsx"
readXlsxFile(path)

13返回整體xlsx數據

#  xlsx   xls
# openpyxl  ->  xlsxfrom openpyxl.reader.excel import load_workbookdef readXlsxFile(path):dic = {}file = load_workbook(filename=path)sheets = file.get_sheet_names()print(len(sheets))for sheetName in sheets:sheet = file.get_sheet_by_name(sheetName)#一張表的所有數據sheetInfo = []for lineNum in range(1, sheet.max_row + 1):lineList = []for columnNum in range(1, sheet.max_column + 1):value = sheet.cell(row=lineNum, column=columnNum).valuelineList.append(value)sheetInfo.append(lineList)#將一張表的數據存到字典dic[sheetName] = sheetInforeturn dic#不能處理xls文件
path = r""
dic = readXlsxFile(path)
print(dic["安力博發"])
print(len(dic))

14返回xls和xlsx文件內容

#有序字典
from collections import OrderedDict
#讀取數據
from pyexcel_xls import get_datadef readXlsAndXlsxFile(path):dic = OrderedDict()#抓取數據xdata = get_data(path)for sheet in xdata:dic[sheet]= xdata[sheet]return dicpath= r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\5、excel自動化辦公\1.xlsx"
dic = readXlsAndXlsxFile(path)
print(dic)
print(len(dic))

15寫入xls文件

#有序字典
from collections import OrderedDict
#讀取數據
from pyexcel_xls import get_data
from pyexcel_xls import save_datadef makeExcelFile(path,data):dic = OrderedDict()for sheetName,sheetValue in data.items():d= {}d[sheetName]=sheetValuedic.update(d)save_data(path,dic)#只能寫xls
path= r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\5、excel自動化辦公\11.xls"
makeExcelFile(path,{"表1":[[1,2,3],[4,5,6]],"表2":[[11,22,33],[44,55,66]]})

16寫ppt

import win32com
import win32com.clientdef makeppt(path):ppt = win32com.client.Dispatch("PowerPoint.Application")ppt.Visible=True#增加一個文件pptFile = ppt.Presentations.Add()#創建頁  參數1為頁數 參數2為主題類型page1 = pptFile.Slides.Add(1,1)#正標題副標題 就兩個t1 = page1.Shapes[0].TextFrame.TextRanget1.Text = "Liuwang "t2 = page1.Shapes[1].TextFrame.TextRanget2.Text = "Liuwang is a good man  "# 第二頁page2 = pptFile.Slides.Add(2, 2)t3 = page2.Shapes[0].TextFrame.TextRanget3.Text = "LiuGE "t4 = page2.Shapes[1].TextFrame.TextRanget4.Text = "LiuGE is a good man  "#保存pptFile.SaveAs(path)pptFile.Close()ppt.Quit()path = r"D:\xiazaipan\第1章  Python語言基礎\15、自動化辦公與鼠標鍵盤模擬\4、word自動化辦公"
makeppt(path)

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/456934.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/456934.shtml
英文地址,請注明出處:http://en.pswp.cn/news/456934.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python爬蟲:一些常用的爬蟲技巧總結

1、基本抓取網頁 get方法 import urllib2 url "http://www.baidu.com" respons urllib2.urlopen(url) print response.read() post方法 import urllib import urllib2url "http://abcde.com" form {name:abc,password:1234} form_data urllib.urlenco…

微型計算機選用要點,微型計算機原理以及應用考試_new要點分析.doc

微型計算機原理以及應用第一章:1.微機的主要的特點是:(1)體積小、重量輕;(2)價格低廉;(3)可靠性高、結構靈活(4)應用面廣2.微型機的分類:按微處理器規模分類:單片機 、個人計算機、 …

到底什么是API經濟

編者按:這是一篇兩年前的文章,作者為原CA TECH的中國區技術總監。他在文章中闡述的問題,今天讀來依舊讓人振聾發聵。但遺憾的是,國人在API成為一種服務的概念上似乎還停留在遙遠的PC時代,說白了還都只是一些低端的數據…

解決Linux下vi或vim操作Found a swap file by the name

在linux下用vi或vim打開 文件時 E325: ATTENTION Found a swap file by the name ".1.py.swp" owned by: liu dated: Sat Apr 20 17:37:19 2019 file name: ~liu/1.py modified: YES user name: liu host name: localhos…

給未來的自己一封信計算機,給未來的自己的一封信范文(精選5篇)

給未來的自己的一封信范文(精選5篇)在日常生活或是工作學習中,大家總免不了要接觸或使用書信吧,書信一般包括稱呼、問候語、正文、祝語、署名、日期六個部分。你知道書信怎樣寫才規范嗎?下面是小編為大家收集的給未來的自己的一封信范文(精選…

matlab神經網絡函數

1.設計函數 solvein 設計線性網絡; solverb 設計徑向基網絡; solverbe 設計精確的徑向基網絡; solvehop 設計Hopfield網絡。 2.傳遞函數 hardlim 硬限幅傳遞函數; hardl…

GBDT算法簡介

在網上看到一篇GBDT介紹非常好的文章,GBDT大概是非常好用又非常好用的算法之一了吧(哈哈 兩個好的意思不一樣) GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Regression Tree),是一種迭代的決策樹算法,該算…

DevExpress Chart空間Y軸歸一化(線性歸一化函數)

數據的標準化(normalization)是將數據按比例縮放,使之落入一個小的特定區間。在某些比較和評價的指標處理中經常會用到,去除數據的單位限制,將其轉化為無量綱的純數值,便于不同單位或量級的指標能夠進行比較…

Linux samba的配置和使用

推薦局域網內使用 不推薦遠程服務器 一、安裝Samba服務 yum -y install samba # 查看yum源中Samba版本 yum list | grep samba # 查看samba的安裝情況 rpm -qa | grep samba Samba服務器安裝完之后, 會生成配置文件目錄/etc/samba, /etc/samba/smb.conf是samba的核心配置文件.…

23期PHP基礎班第四天

轉載于:https://www.cnblogs.com/lihang666/p/6078982.html

SVM和SVR簡介

1、支持向量機( SVM )是一種比較好的實現了結構風險最小化思想的方法。它的機器學習策略是結構風險最小化原則 為了最小化期望風險,應同時最小化經驗風險和置信范圍) 支持向量機方法的基本思想: ( 1 &#…

gojs實現最短路徑尋址實例

2019獨角獸企業重金招聘Python工程師標準>>> JS function init() {if (window.goSamples) goSamples(); // init for these samples -- you dont need to call thisvar $ go.GraphObject.make; // for conciseness in defining templatesmyDiagram $(go.Diagram,…

河南王牌計算機專業,河南計算機專業實力突出的7所大學,鄭大位列次席,榜首實至名歸...

鄭州大學是省內唯一的211建設高校,整體辦學實力在國內同類高校之中名列前茅,雖然沒有能夠在學科評估之中取得A類學科,但學校有化學、考古學、材料科學與工程等多個學科獲評B,學校計算機科學與技術學科取得了C的成績,雖…

Linux中配置ftp服務器

1. 先用rpm -qa| grep vsftpd命令檢查是否已經安裝,如果ftp沒有安裝,使用yum -y install vsftpd 安裝,(ubuntu 下使用apt-get install vsftpd) 2. service vsftpd start / service vsftpd restart 啟動要讓FTP每次開機自動啟動,運行命令:…

機器學習中各類算法的優缺點比較

1決策樹(Decision Trees)的優缺點 決策樹的優點: 一、 決策樹易于理解和解釋.人們在通過解釋后都有能力去理解決策樹所表達的意義。 二、 對于決策樹,數據的準備往往是簡單或者是不必要的.其他的技術往往要求先把數據一般化&am…

在程序開發中日志級別

日志打印可以查看代碼的執行情況,以及快速定位錯誤。 在代碼中,特別是業務層邏輯的代碼,適當的添加日志是必須的,一般在catch代碼塊中是出現異常的,如果需要打印 可以用error級別, 一般的無關緊要的日志&am…

基于Python搭建Django后臺管理系統

一、博客網站的創建 創建項目 生成站點(sites)Model,這兩步驟第一篇有介紹,這里就直接操作了 二、數據庫配置 介紹一下數據庫的配置就是在setting里面配置鏈接的數據庫,這里系統以及配置好了,鏈接一個…

計算機研究所專業課,【擇校必看】十三所計算機專業課只考數據結構的985院校!...

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓敲黑板:本文涉及到的學校計算機專業考研只考數據結構,其中部分院校同時也會考算法、C語言等相關內容。但是,相對其他幾門,無疑在專業課的復習上大大降低了難度。如果各位同學目前的專…

在Python2.7下如何安裝TA-lib庫

最近在做一個關于股票預測的模型,由于想要用Talib庫中的方法,來提取各種金融技術指標,所以就下了這個庫。但整個過程可謂是一波三折。花費了大半天才搞定這件事。 下面來給大家分享一下安裝的步驟,省的大家再往這個坑里跳。。。 …

JavaScript 實現繼承的5種方式

js是一個面向對象的語言,所以具備一些面向對象的方式----------例如繼承。接下來介紹5種js的繼承方式.注意:js 中的函數其實是對象,函數名是對 Function 對象的引用。 1.采用call方法改變函數上下文實現繼承,原理是改變函數內部的…