Python八個(gè)自動(dòng)化辦公的技巧
導(dǎo)語(yǔ)
哈嘍吖鐵汁萌!今天這期就給大家介紹幾個(gè)我用到的辦公室自動(dòng)化技巧,可以瞬速提高辦公效率。有需要的可以往下滑了
1、Word文檔doc轉(zhuǎn)docx
去年想?yún)①愐粋€(gè)數(shù)據(jù)比賽, 里面的數(shù)據(jù)都是doc格式, 想用python-docx 讀取word文件中的數(shù)據(jù), 但是python-docx只支持docx格式, 所以研究了這兩種格式的轉(zhuǎn)換。
1.1 導(dǎo)入工具包
import os from win32com import client as wc
1.2 獲取文件夾下面所有doc文件明細(xì)
# 路徑 path="C:/Users/yyz/Desktop/python辦公技巧/data/doc轉(zhuǎn)docx/" # 根據(jù)自己電腦文件修改 # 定義空l(shuí)ist,存放文件絕對(duì)路徑 files = [] for file in os.listdir(path): if file.endswith(".doc"): files.append(path+file) files
1.3 轉(zhuǎn)換文件
# 運(yùn)行word程序 word = wc.Dispatch("Word.Application") # for循環(huán) i = 0 for file in files: try: doc = word.Documents.Open(file) #打開(kāi)word文件 doc.SaveAs("{}x".format(file), 12) #另存為后綴為".docx"的文件,其中參數(shù)12指docx文件 doc.Close() #關(guān)閉原來(lái)word文件 print(file +':轉(zhuǎn)換成功') i +=1 except: print(file +':轉(zhuǎn)換[不成功]') files.append(file) # 若讀取文件報(bào)錯(cuò), 則將文件名稱添加到files列表中重新讀取 pass print('轉(zhuǎn)換文件%i個(gè)'%i) # 退出word word.Quit()
2、文字地址批量轉(zhuǎn)經(jīng)緯度
工作中地址轉(zhuǎn)經(jīng)緯度會(huì)用在做地圖可視化或者計(jì)算距離方面。
2.1 導(dǎo)入工具包
# 導(dǎo)入工具包 import pandas as pd import json from urllib.request import urlopen, quote import requests
2.2 定義轉(zhuǎn)換函數(shù)
# 定義函數(shù) def getlnglat(address): url = 'http://api.map.baidu.com/geocoding/v3/' output = 'json' ak = "自己申請(qǐng)的api" # 百度地圖API, 需要自己申請(qǐng) address = quote(address) # 由于本文地址變量為中文,為防止亂碼,先用quote進(jìn)行編碼 uri = url + '?' + 'address=' + address + '&output=' + output + '&ak=' + ak +'&callback=showLocation%20'+'//GET%E8%AF%B7%E6%B1%82' res=requests.get(uri).text temp = json.loads(res) # 將字符串轉(zhuǎn)化為json lat = temp['result']['location']['lat'] lng = temp['result']['location']['lng'] return lng, lat # 經(jīng)度 longitude,緯度 latitude,
2.3 地址轉(zhuǎn)換
2.3.1 單個(gè)地址轉(zhuǎn)換
# 單個(gè)地址轉(zhuǎn)換 getlnglat('北京市朝陽(yáng)區(qū)高碑店地區(qū)辦事處高井村委會(huì)') (116.52784003604923, 39.91806508560947)
2.3.2 批量地址轉(zhuǎn)換
# 讀取數(shù)據(jù) data = pd.read_excel('C:/Users/yyz/Desktop/python辦公技巧/data/地址信息.xlsx') data
data['經(jīng)度'] = '' data['緯度'] = '' for i in range(data.shape[0]): ? ? try: ? ? ? ? data.iloc[i,2] = getlnglat(data.iloc[i,1])[0] # 經(jīng)度 將第i行,第2列的地址(列索引為1)轉(zhuǎn)換為經(jīng)緯度,并將經(jīng)度賦值給第i行,第3列(列索引為2) ? ? ? ? data.iloc[i,3] = getlnglat(data.iloc[i,1])[1] # 緯度 ? ? except: ? ? ? ? pass ? ? #print(i) data
3、經(jīng)緯度計(jì)算距離
安裝工具包
pip install geopy
3.1 導(dǎo)入工具包
from geopy.distance import geodesic
3.2 讀取數(shù)據(jù)
# 讀取數(shù)據(jù) data = pd.read_excel('C:/Users/yyz/Desktop/python辦公技巧/data/經(jīng)緯度計(jì)算距離.xlsx') data
3.3 計(jì)算距離
# 將經(jīng)緯度賦值給變量,簡(jiǎn)化 wd1 = data['緯度1'].tolist() jd1 = data['經(jīng)度1'].tolist() wd2 = data['緯度2'].tolist() jd2 = data['經(jīng)度2'].tolist() lis1 = [] for i in range(len(data)): j= geodesic((wd1[i],jd1[i]), (wd2[i],jd2[i])).km # 緯度 經(jīng)度 緯度 經(jīng)度 lis1.append(j) #print(i) data['距離'] = lis1 data
4、百度經(jīng)緯度轉(zhuǎn)高德經(jīng)緯度
公司有2個(gè)系統(tǒng),用的坐標(biāo)系不一樣, 有時(shí)候需要轉(zhuǎn)換一下
4.1 工具包
# 導(dǎo)入工具包 import math import pandas as pd
4.2 定義函數(shù)
# 定義轉(zhuǎn)換函數(shù) def bdToGaoDe(lon,lat): PI = 3.14159265358979324 * 3000.0 / 180.0 x = lon - 0.0065 y = lat - 0.006 z = math.sqrt(x * x + y * y) - 0.00002 * math.sin(y * PI) theta = math.atan2(y, x) - 0.000003 * math.cos(x * PI) lon = z * math.cos(theta) lat = z * math.sin(theta) return lon,lat
4.3 單個(gè)轉(zhuǎn)換
# 單個(gè)轉(zhuǎn)換 bdToGaoDe(116.512885, 39.847469) (116.50647396357492, 39.84120409781157)
4.4 批量轉(zhuǎn)換
# 讀取數(shù)據(jù) data = pd.read_excel('C:/Users/yyz/Desktop/python辦公技巧/data/百度經(jīng)緯度轉(zhuǎn)高德.xlsx') data.head()
wd = data['緯度'].tolist() jd = data['經(jīng)度'].tolist() # 定義一個(gè)空列表 li1 = [] for i in range(len(data)): ? ? j ?= bdToGaoDe(jd[i],wd[i]) ? ? li1.append(j) ? ?? li1 data['經(jīng)度_re'] = [i[0] for i in li1] data['緯度_re'] = [i[1] for i in li1] data.head()
5、Excel文件批量合并
5.1 工具包
# 導(dǎo)入工具包 import pandas as pd import os
5.2 獲取文件列表
# 設(shè)置文件路徑 path = 'C:/Users/yyz/Desktop/python辦公技巧/data/數(shù)據(jù)合并/' # 空列表, 用于存放文件路徑 files = [] for file in os.listdir(path): if file.endswith(".xlsx"): files.append(path+file) # 查看列表 files
5.3 轉(zhuǎn)換存儲(chǔ)數(shù)據(jù)
# 定義一個(gè)空的dataframe data = pd.DataFrame() # 遍歷所有文件 for file in files: datai = pd.read_excel(file) datai_len = len(datai) data = data.append(datai) # 添加到總的數(shù)據(jù)中 print('讀取%i行數(shù)據(jù),合并后文件%i列, 名稱:%s'%(datai_len,len(data.columns),file.split('/')[-1])) # 查看是否全部讀取,格式是否出錯(cuò) # 重置索引 data.reset_index(drop=True,inplace=True)
6、Word文件批量轉(zhuǎn)pdf
只能轉(zhuǎn)docx文件,轉(zhuǎn)doc文件會(huì)報(bào)錯(cuò), 工具包安裝
pip install docx2pdf
6.1 導(dǎo)入工具包???????
# 安裝工具包: # 導(dǎo)入工具包 from docx2pdf import convert import os
6.2 單個(gè)轉(zhuǎn)換
# 單個(gè)轉(zhuǎn)換 convert("c:/users/yyz/desktop/魔方公式.docx", "c:/users/yyz/desktop/excel筆記.pdf")
6.3 批量轉(zhuǎn)換???????
# 文件位置 path = 'C:/Users/yyz/Desktop/python辦公技巧/data/word轉(zhuǎn)pdf/' # 定義空l(shuí)ist,存放文件列表 files = [] for file in os.listdir(path): if file.endswith(".docx"): files.append(path+file) files for file in files: convert(file,file.split('.')[0]+'.pdf') print(file+'轉(zhuǎn)換成功')
7、批量讀取word中表格數(shù)據(jù)
7.1工具包安裝
pip install python-docx
# 讀取word文件 doc = docx.Document('C:/Users/yyz/Desktop/python辦公技巧/data/word信息.docx') # 獲取文檔中所有表格對(duì)象的列表 biaoges = doc.tables
7.2 不規(guī)范的表格???????
cells = biaoges[1]._cells cells_lis = [[cell.text for cell in cells]]
import pandas as pd import numpy as np datai = pd.DataFrame(cells_lis) datai = datai[[1,3,7,9,14,16,19,21]] datai.columns = ['姓名','年齡','籍貫','住址','工作單位','電話','是否黨員','出生日期'] datai
7.3 規(guī)范數(shù)據(jù)???????
# 獲取第1個(gè)表格行丨 rowi = len(biaoges[0].rows) rowi
# 定義空列表 lis1 = [] # for循環(huán)獲取第一個(gè)表的數(shù)據(jù) for i in range(1,rowi): # 從第2行開(kāi)始循環(huán) lis1.append([biaoges[0].cell(i,0).text, biaoges[0].cell(i,1).text, biaoges[0].cell(i,2).text, biaoges[0].cell(i,3).text, biaoges[0].cell(i,4).text])
# 創(chuàng)建一個(gè)dataframe data1 = pd.DataFrame(lis1,columns=['日期','品類','數(shù)量','價(jià)格','金額']) data1
7.4 批量讀取???????
import pandas as pd import os os.chdir('C:/Users/yyz/Desktop/python辦公技巧/data/word信息/')
lis1=[] for file in os.listdir('.'): if file.endswith('.docx'): doc = docx.Document('./'+file) biaoges = doc.tables rowi = len(biaoges[0].rows) for i in range(1,rowi): lis1.append([biaoges[0].cell(i,0).text, biaoges[0].cell(i,1).text, biaoges[0].cell(i,2).text, biaoges[0].cell(i,3).text, biaoges[0].cell(i,4).text])
# 創(chuàng)建dataframe data1 = pd.DataFrame(lis1,columns=['日期','品類','數(shù)量','價(jià)格','金額']) data1
8 用outlook批量發(fā)郵件
8.1 導(dǎo)入工具包???????
import win32com.client as win32 import pandas as pd
8.2 讀取數(shù)據(jù)
# 讀取數(shù)據(jù) data1 = pd.read_excel('C:/Users/yyz/Desktop/python批量發(fā)送郵件.xlsx',sheet_name='發(fā)送郵件') data1.fillna('',inplace=True)
8.3 發(fā)送郵件
# 運(yùn)行outlook outlook = win32.Dispatch("outlook.Application") # for循環(huán)發(fā)送文件 for i in range(data1.shape[0]): mail = outlook.CreateItem(0) # 創(chuàng)建一個(gè)郵件對(duì)象 win32.constants.olMailItem mail.To = data1.iloc[i,0] #收件人 mail.CC = data1.iloc[i,1] #抄送人 mail.Subject = data1.iloc[i,2] #郵件主題 mail.HTMLBody = data1.iloc[i,3] # 郵件正文 html格式 # mail.Body = data1.iloc[i,3] # 郵件正文 mail.Attachments.Add(data1.iloc[i,4]) # 附件 mail.Send() #發(fā)送 i +=1 print('發(fā)送郵件%i份'%i)
python辦公自動(dòng)化的技巧還有很多, python好掌握,能幫助我們提升工作效率,這也是很多非編程人員學(xué)習(xí)python的原因之一。
以上就是Python七個(gè)自動(dòng)化辦公的技巧的詳細(xì)內(nèi)容,更多關(guān)于Python自動(dòng)化辦公的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 在threading中如何處理主進(jìn)程和子線程的關(guān)系
這篇文章主要介紹了python 在threading中如何處理主進(jìn)程和子線程的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python在回調(diào)函數(shù)中獲取返回值的方法
今天小編就為大家分享一篇python在回調(diào)函數(shù)中獲取返回值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Django中ORM表的創(chuàng)建和增刪改查方法示例
這篇文章主要給大家介紹了關(guān)于Django中ORM表的創(chuàng)建和增刪改查等基本操作的方法,還給大家分享了django orm常用查詢篩選的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11解決Python 寫(xiě)文件報(bào)錯(cuò)TypeError的問(wèn)題
這篇文章主要介紹了解決Python 寫(xiě)文件報(bào)錯(cuò)TypeError的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10python中循環(huán)語(yǔ)句while用法實(shí)例
這篇文章主要介紹了python中循環(huán)語(yǔ)句while用法,實(shí)例分析了while語(yǔ)句的使用方法,需要的朋友可以參考下2015-05-05Python Numpy教程之排序,搜索和計(jì)數(shù)詳解
這篇文章主要為大家詳細(xì)介紹了Python?NumPy中排序,搜索和計(jì)數(shù)的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2022-08-08pandas實(shí)現(xiàn)數(shù)據(jù)合并的示例代碼
本文主要介紹了pandas實(shí)現(xiàn)數(shù)據(jù)合并的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05