一、读写CSV文件
1、读CSV文件
import csv
def readCsv ( path) :
infoList = [ ]
with open ( path, "r" ) as f:
allFileInfo = csv. reader( f)
for row in allFileInfo:
infoList. append( row)
return infoList
path = r"E:\pythonProject\object\first\40、自动化办公\1、读写CSV文件\000001.csv"
info = readCsv( path)
print ( info)
2、写CSV文件
import csv
def writeCsv ( path, data) :
with open ( path, "w" ) as f:
writer = csv. writer( f)
for rowData in data:
writer. writerow( rowData)
path = r"E:\pythonProject\object\first\40、自动化办公\1、读写CSV文件\000002.csv"
data = [ [ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , 9 ] ]
writeCsv( path, data)
二、读取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 PDFTextExtractionNotAllowed
def readPDF ( path, toPath) :
with open ( path, "rb" ) as f:
parser = PDFParser( f)
pdfFile = PDFDocument( )
parser. set_document( pdfFile)
pdfFile. set_parser( parser)
pdfFile. initialize( )
if not pdfFile. is_extractable:
raise PDFTextExtractionNotAllowed
else :
manager = PDFResourceManager( )
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+" , encoding= "utf-8" ) as f:
str = x. get_text( )
print ( str )
f. write( str + "\n" )
path = r"E:\pythonProject\object\first\40、自动化办公\2、读取PDF文件\面向对象简介及思想.pdf"
toPath = r"E:\pythonProject\object\first\40、自动化办公\2、读取PDF文件\n.txt"
readPDF( path, toPath)
三、word自动化办公
1、读取doc和docx文件
import win32com
import win32com. client
def readWoldFile ( path) :
mw = win32com. client. Dispatch( "Word.Application" )
doc = mw. Documents. Open( path)
for paragraph in doc. Paragraphs:
line = paragraph. Range. Text
print ( line)
doc. Close( )
mw. Quit( )
path = r"E:\pythonProject\object\first\Python.docx"
readWoldFile( path)
2、读取doc与docx文件并写入其他文件
import win32com
import win32com. client
def readWoldFileToAnotherFile ( path, toPath) :
mw = win32com. client. Dispatch( "Word.Application" )
doc = mw. Documents. Open( path)
doc. SaveAs( toPath, 2 )
doc. Close( )
mw. Quit( )
path = r"E:\pythonProject\object\first\40、自动化办公\3、Word自动化办公\stu.doc"
toPath = r"E:\pythonProject\object\first\40、自动化办公\3、Word自动化办公\a.txt"
readWoldFileToAnotherFile( path, toPath)
3、创建word文件
import win32com
import win32com. client
import os
def makeWordFile ( path, name) :
word = win32com. client. Dispatch( "Word.Application" )
word. Visible = True
doc = word. Documents. Add( )
r = doc. Range( 0 , 0 )
r. InsertAfter( "亲爱的" + name + "\n" )
r. InsertAfter( " 我想你\n" )
doc. SaveAs( path)
doc. Close( )
word. Quit( )
names = [ "张三" , "李四" , "王五" ]
for name in names:
path = os. path. join( os. getcwd( ) , name)
makeWordFile( path, name)
四、excel自动化办公
1、读取elxs文件
from openpyxl. reader. excel import load_workbook
def readXlsxFile ( path) :
file = load_workbook( filename= path)
sheets = file . get_sheet_names( )
sheet = file . get_sheet_by_name( sheets[ 0 ] )
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) . value
if value != None :
lineList. append( value)
print ( lineList)
path = r"E:\pythonProject\object\first\40、自动化办公\4、Excel自动化办公\1.xlsx"
readXlsxFile( path)
2、返回整体xlsx文件
from openpyxl. reader. excel import load_workbook
def readXlsxFile ( path) :
dic = { }
file = load_workbook( filename= path)
sheets = file . get_sheet_names( )
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) . value
if value != None :
lineList. append( value)
sheetInfo. append( lineList)
dic[ sheetName] = sheetInfo
return dic
path = r"E:\pythonProject\object\first\40、自动化办公\4、Excel自动化办公\1.xlsx"
dic = readXlsxFile( path)
print ( dic)
print ( len ( dic) )
3、返回xls与xlsx文件的内容
from collections import OrderedDict
from pyexcel_xls import get_data
def readXlsAndXlsxFile ( path) :
dic = OrderedDict( )
xdata = get_data( path)
for sheet in xdata:
dic[ sheet] = xdata[ sheet]
return dic
path = r"E:\pythonProject\object\first\40、自动化办公\4、Excel自动化办公\1.xlsx"
dic = readXlsAndXlsxFile( path)
print ( dic)
print ( len ( dic) )
4、写入xls文件
from collections import OrderedDict
from pyexcel_xls import save_data
def makeExcel ( path, data) :
dic = OrderedDict( )
for sheetName, sheetValue in data. items( ) :
d = { }
d[ sheetName] = sheetValue
dic. update( d)
save_data( path, dic)
path = r"E:\pythonProject\object\first\40、自动化办公\4、Excel自动化办公\b.xls"
makeExcel( path, { "表1" : [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] , "表2" : [ [ 11 , 22 , 33 ] , [ 44 , 55 , 66 ] , [ 77 , 88 , 99 ] ] } )
五、ppt自动化办公
写ppt
import win32com
import win32com. client
import os
def makePPT ( path) :
ppt = win32com. client. Dispatch( "PowerPoint.Application" )
ppt. Visible = True
pptFile = ppt. Presentations. Add( )
page1 = pptFile. Slides. Add( 1 , 1 )
t1 = page1. Shapes[ 0 ] . TextFrame. TextRange
t1. Text = "fanfan"
t2 = page1. Shapes[ 1 ] . TextFrame. TextRange
t2. Text = "fanfan is a good man"
page2 = pptFile. Slides. Add( 2 , 2 )
t3 = page2. Shapes[ 0 ] . TextFrame. TextRange
t3. Text = "凯哥"
t4 = page2. Shapes[ 1 ] . TextFrame. TextRange
t4. Text = """kaige is a good man
kaige is a nice man
kaige is a cool man"""
for i in range ( 3 , 6 ) :
page = pptFile. Slides. Add( 2 , 2 )
t3 = page. Shapes[ 0 ] . TextFrame. TextRange
t3. Text = "凯哥"
t4 = page. Shapes[ 1 ] . TextFrame. TextRange
t4. Text = """kaige is a good man\nkaige is a nice man\nkaige is a cool man"""
pptFile. SaveAs( path)
pptFile. Close( )
ppt. Quit( )
path = r"E:\pythonProject\object\first\40、自动化办公\5、PPT自动化办公\fanfan.ppt"
makePPT( path)
六、播放音乐
import time
import pygame
filePath= r"E:\pythonProject\object\first\40、自动化办公\6、播放音乐\赵英俊 - 送你一朵小红花.mp3"
pygame. mixer. init( )
track = pygame. mixer. music. load( filePath)
pygame. mixer. music. play( )
time. sleep( 20 )
pygame. mixer. music. stop( )
七、修改背景图片
import win32api
import win32con
import win32gui
def setWallPaper ( path) :
reg_key = win32api. RegOpenKeyEx( win32con. HKEY_CURRENT_USER, "Control Panel\\Desktop" , 0 , win32con. KEY_SET_VALUE)
win32api. RegSetValueEx( reg_key, "WallpaserStyle" , 0 , win32con. REG_SZ, "2" )
win32gui. SystemParametersInfo( win32con. SPI_SETDESKWALLPAPER, path, win32con. SPIF_SENDWININICHANGE)
setWallPaper( r"D:\壁纸\SogouWP\Local\WallPaper\1.jpg" )