1. 程式人生 > 其它 >Python語法進階(4)- Python辦公自動化

Python語法進階(4)- Python辦公自動化

1.Python檔案自動化管理

shutil檔案操作模組;glob檔案與資料夾獲取模組

1.1.檔案操作利器-shutil

1.1.1.檔案的複製

 1 # coding:utf-8
 2 import os
 3 from shutil import copy
 4 
 5 #相對路徑
 6 copy('test.txt','test')
 7 
 8 #絕對路徑
 9 path=os.path.join(os.getcwd(),'test1.txt')
10 target = os.path.join(os.getcwd(), 'test')
11 copy(path,target)
12 13 #通過copy實現檔案內容覆蓋 14 copy('test.txt','test2.txt')

1.1.2.檔案內容的覆蓋

1 # coding:utf-8
2 import os
3 from shutil import copy,copyfile
4 
5 #相對路徑
6 copyfile('test.txt','test1.txt')

1.1.3.檔案裁剪(移動,重新命名)

1 # coding:utf-8
2 
3 from shutil import move
4 
5 move('test2.txt','test/test22.txt')

1.1.4.檔案的刪除

1 # coding:utf-8
2 
3 import os
4 
5 os.remove('test1.txt')

1.1.5.檔案/資料夾的壓縮

1 # coding:utf-8
2 import os
3 from shutil import make_archive
4 
5 make_archive('test','zip',os.getcwd())

1.1.6.檔案/資料夾的解壓縮

1 # coding:utf-8
2 import os
3 from shutil import make_archive,unpack_archive
4 
5 unpack_archive('
test.zip',os.path.join(os.getcwd(),'test2'))

1.2.資料夾操作利器-shutil

1.2.1.資料夾的複製

1 # coding:utf-8
2 
3 from shutil import copytree
4 
5 copytree('test','test2') #copytree目標目錄不能存在,不然報錯

1.2.2.資料夾的刪除

1 # coding:utf-8
2 
3 from shutil import rmtree
4 
5 rmtree('test2')#刪除的資料夾必須要存在,不然會報錯,資料夾中不管有沒有檔案都可以直接刪除

1.2.3.資料夾的裁剪(移動,重新命名)

1 # coding:utf-8
2 
3 from shutil import move
4 
5 move('test1','test')#當目標路徑不存在,並且和來源目錄屬於相同路徑下,相當於重新命名操作

1.3.檔案內容的查詢:glob

1.3.1.glob的介紹

1.3.2.glob的基本使用

 1 # coding:utf-8
 2 import os
 3 from glob import glob
 4 
 5 print(glob(os.getcwd()))    #['D:\\WorkSpace\\Python_Study\\filetest'] 只返回了當前路徑,因為沒有給範圍
 6 
 7 result=glob(os.getcwd()+'/*')   #通過正則表示式來匹配
 8 #也可以模糊匹配 result=glob(os.getcwd()+'/*.txt')
 9 print(result)
10 '''
11 [
12 'D:\\WorkSpace\\Python_Study\\filetest\\file_test.py', 
13 'D:\\WorkSpace\\Python_Study\\filetest\\filrtree_test.py', 
14 'D:\\WorkSpace\\Python_Study\\filetest\\test', 
15 'D:\\WorkSpace\\Python_Study\\filetest\\test.txt', 
16 'D:\\WorkSpace\\Python_Study\\filetest\\test1', 
17 'D:\\WorkSpace\\Python_Study\\filetest\\__init_
18 '''

1.3.3.練習1:查詢指定的檔案

 1 # coding:utf-8
 2 
 3 import glob
 4 
 5 # 獲取當前路徑下所有內容
 6 # 判斷每個內容的型別(資料夾還是檔案)
 7 # 遞迴
 8 
 9 path= glob.os.path.join(glob.os.getcwd(),'*')
10 
11 def search(path,target):
12     result = glob.glob(path)
13     final_result=[]
14 
15     for data in result:
16         if glob.os.path.isdir(data):
17             _path=glob.os.path.join(data,"*")
18             search(_path,target)
19         else:
20             if target in data:
21                 final_result.append(data)
22     return final_result
23 
24 if __name__=='__main__':
25     result=search(path,target='.py')
26     print(result)
27 
28 '''
29 ['D:\\WorkSpace\\Python_Study\\filetest\\file_test.py', 
30 'D:\\WorkSpace\\Python_Study\\filetest\\filrtree_test.py', 
31 'D:\\WorkSpace\\Python_Study\\filetest\\__init__.py']
32 '''

1.3.4.練習2:查詢含有指定內容的檔案

 1 # coding:utf-8
 2 
 3 import glob
 4 
 5 path= glob.os.path.join(glob.os.getcwd(),'*')
 6 final_result=[]
 7 
 8 def search(path,target):
 9     result = glob.glob(path)
10 
11     for data in result:
12         if glob.os.path.isdir(data):
13             _path=glob.os.path.join(data,"*")
14             search(_path,target)
15         else:
16             try:
17                 with open(data,'r',encoding='utf-8') as f:
18                     content=f.read()
19                 if target in content:
20                     final_result.append(data)
21             except:
22                 print('data read failed:%s'% data)
23 
24     return final_result
25 
26 if __name__=='__main__':
27     result=search(path,target='你好')
28     print(result)
29 
30 '''
31 ['D:\\WorkSpace\\Python_Study\\filetest\\filrtree_test.py',
32  'D:\\WorkSpace\\Python_Study\\filetest\\test\\test22.txt', 
33  'D:\\WorkSpace\\Python_Study\\filetest\\test.txt']
34 '''

1.3.5.練習3:清理重複檔案

 1 # coding:utf-8
 2 
 3 import glob
 4 import hashlib
 5 
 6 data={}
 7 def clear(path):
 8     result=glob.glob(path)
 9 
10     for _data in result:
11         if glob.os.path.isdir(_data):
12             _path=glob.os.path.join(_data,'*')
13             clear(_path)
14         else:
15             name=glob.os.path.split(_data)[-1]
16             if 'zip' in name:
17                 open_type='rb'
18                 is_byte=True
19             else:
20                 open_type='r'
21                 is_byte = False
22             with open(_data,open_type) as f:
23                 content = f.read()
24                 if is_byte:
25                     hash_content_obj = hashlib.md5(content)
26                 else:
27                     hash_content_obj = hashlib.md5(content.encode('utf-8'))
28                 hash_content = hash_content_obj.hexdigest()
29             if name in data:
30                 sub_data=data[name]
31                 is_delete=False
32                 for k,v in sub_data:
33                     if v == hash_content:
34                         glob.os.remove(_data)
35                         print('%s will delete' % _data)
36                         is_delete=True
37                 if not is_delete:
38                     data[name][_data]=hash_content
39             else:
40                 data[name]= {
41                     _data:hash_content
42                 }
43 
44 if __name__=='__main__':
45     path = glob.os.path.join(glob.os.getcwd(),'*')
46     clear(path)
47     print(data)

1.3.6.練習4:批量修改目錄中的檔名稱

 1 # coding:utf-8
 2 
 3 import glob
 4 import shutil
 5 
 6 
 7 def update_name(path):
 8     result = glob.glob(path)
 9 
10     for index,data in enumerate(result):
11         if glob.os.path.isdir(data):
12             _path = glob.os.path.join(data,'*')
13             update_name(_path)
14         else:
15             path_list=glob.os.path.split(data)
16             name=path_list[-1]
17             new_name='%s_%s' % (index,name)
18             new_data=glob.os.path.join(path_list[0],new_name)
19             shutil.move(data,new_data)
20 
21 if __name__=='__main__':
22     path = glob.os.path.join(glob.os.getcwd(),'*')
23     update_name(path)

 

2.Python Word自動化

2.1.Word操作利器之python-docx

2.1.1.python-docx的源頭Document

2.1.2.段落的讀取

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 
 5 doc=Document('文字.docx')    #如果不是docx檔案用不了,需要轉格式即doc->docx
 6 print(doc.paragraphs)   #[<docx.text.paragraph.Paragraph object at 0x0000019CB5E75A48>, <docx.text.paragraph.Paragraph object at 0x0000019CB5E75F08>, <docx.text.paragraph.Paragraph object at 0x0000019CB5E75948>, <docx.text.paragraph.Paragraph object at 0x0000019CB5E75EC8>, <docx.text.paragraph.Paragraph object at 0x0000019CB5E75B48
 7 
 8 for p in doc.paragraphs:
 9     print(p.text)
10 '''
11 paragraphs是讀取不到圖片以及表格等資訊的,因為讀取的是段落
12 文字
13 文字
14 文字
15 文字
16 文字
17 ''
1 # coding:utf-8
2 
3 from docx import Document
4 #獲取word總行數,並輸出每一行內容
5 doc =Document('文字.docx')
6 leng_doc=len(doc.paragraphs)
7 print('段落數:%s' % leng_doc)
8 for i in range(leng_doc):
9     print('第{}段的內容是:{}'.format(i+1,doc.paragraphs[i].text))

2.1.3.表格的讀取

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 
 5 doc=Document('文字.docx')    #如果不是docx檔案用不了,需要轉格式即doc->docx
 6 
 7 #按行去取資料
 8 for t in doc.tables:  #拿到表格
 9     for row in t.rows:  #拿到表格的行
10         _row_str=''     #定義一個變數將一行的資料拼接在一起
11         for cell in row.cells:  #遍歷一行中的小表格即列
12             _row_str+=cell.text+'\t'    #資料拼接
13         print(_row_str)
14 
15 '''
16 1-1    1-2    
17 2-1    2-2    
18 3-1    3-2    
19 '''
20 
21 # 按列去取資料
22 for t in doc.tables:  #拿到表格
23     for row in t.columns:  #拿到表格的行
24         _row_str=''     #定義一個變數將一行的資料拼接在一起
25         for cell in row.cells:  #遍歷一行中的小表格即行
26             _row_str+=cell.text+'\t'    #資料拼接
27         print(_row_str)
28 '''
29 1-1    2-1    3-1    
30 1-2    2-2    3-2    
31 '''

2.1.4.練習:獲取出有效簡歷-簡歷篩選

 1 # coding:utf-8
 2 
 3 import glob
 4 
 5 from docx import Document
 6 
 7 class ReadDoc(object):
 8     def __init__(self, path):
 9         self.doc = Document(path)
10         self.p_text = ''
11         self.table_text = ''
12 
13         self.get_para()
14         self.get_table()
15 
16     def get_para(self):
17         for p in self.doc.paragraphs:
18             self.p_text += p.text + '\n'
19 
20     def get_table(self):
21         for table in self.doc.tables:
22             for row in table.rows:
23                 _cell_str = ''
24                 for cell in row.cells:
25                     _cell_str += cell.text + ','
26                 self.table_text += _cell_str + '\n'
27 
28 
29 def search_word(path, targets):
30     result = glob.glob(path)
31     final_result = []
32 
33     for i in result:
34         isuse = True
35         if glob.os.path.isfile(i):
36             if i.endswith('.docx'):
37                 doc = ReadDoc(i)
38                 p_text = doc.p_text
39                 t_text = doc.table_text
40                 all_text = p_text + t_text
41 
42                 for target in targets:
43                     if target not in all_text:
44                         isuse = False
45                         break
46 
47                 if not isuse:
48                     continue
49                 final_result.append(i)
50     return final_result
51 
52 
53 if __name__ == '__main__':
54     path = glob.os.path.join(glob.os.getcwd(), '*')
55     res = search_word(path, ['簡歷', '1'])
56     print(res)

2.2.生成word文件

2.2.1.生成標題

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 
 5 doc = Document()
 6 
 7 #定義標題內容
 8 title = doc.add_heading('My title',0)
 9 
10 #標題追加
11 title.add_run('\n123')
12 
13 #儲存doc物件 word文件
14 doc.save('test.docx')

2.2.2.新增段落

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 
 5 doc = Document()
 6 
 7 #定義標題內容
 8 title = doc.add_heading('My title',0)
 9 
10 #標題追加
11 title.add_run('\n123')
12 
13 #新增段落
14 p = doc.add_paragraph('這是段落內容')
15 #段落內容追加
16 p.add_run('\n這是追加的段落內容')
17 #儲存doc物件 word文件
18 doc.save('test.docx')

2.2.3.新增圖片

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 from docx.shared import Inches
 5 
 6 doc = Document()
 7 
 8 #新增圖片
 9 image=doc.add_picture('logo2020.png',width=Inches(3),height=Inches(2))
10 
11 #儲存doc物件 word文件
12 doc.save('test.docx')

2.2.4.新增表格

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 
 5 doc = Document()
 6 
 7 title = ['name','age','sex']
 8 
 9 table = doc.add_table(rows=1,cols=3)
10 title_cells = table.rows[0].cells
11 for i in range(len(title)):
12     title_cells[i].text=title[i]
13 
14 data = [
15     ('zhangsan','10','man'),
16     ('lisi','20','man'),
17     ('xiaofang','18','women')
18 ]
19 
20 for d in data:
21     row_cells = table.add_row().cells
22     row_cells[0].text = d[0]
23     row_cells[1].text = d[1]
24     row_cells[2].text = d[2]
25 
26 #儲存doc物件 word文件
27 doc.save('test.docx')

2.2.5.分頁

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 
 5 doc = Document()
 6 
 7 doc.add_heading('第一頁',0)
 8 
 9 #分頁
10 doc.add_page_break()
11 
12 doc.add_heading('第二頁',0)
13 #儲存doc物件 word文件
14 doc.save('test.docx')

2.2.6.儲存生成word

1 #儲存doc物件 word文件
2 doc.save('test.docx')

2.3.word全域性樣式和文字樣式

2.3.1.全域性樣式定義

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 from docx.shared import Inches,RGBColor,Pt
 5 
 6 doc = Document()
 7 
 8 #style的這些操作不會對標題產生影響
 9 style = doc.styles['Normal']
10 style.font.name = '仿宋'  #字型型別
11 style.font.color.rgb = RGBColor(255,0,0)    #字型顏色:紅色
12 style.font.size = Pt(30)    #字型大小:30
13 
14 #標題
15 doc.add_heading('第一頁',0)
16 #段落
17 doc.add_paragraph('這是段落內容')
18 
19 #儲存doc物件 word文件
20 doc.save('test.docx')

2.3.2.文字樣式定義-標題與段落

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 from docx.shared import Inches,RGBColor,Pt
 5 from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
 6 
 7 doc = Document()
 8 
 9 #標題
10 title = doc.add_heading('第一頁',0)
11 title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER     #標題居中
12 title.style.font.size=Pt(30)    #標題字型大小
13 title.italic = True             #一開始的標題內容無法使其斜體格式生效,如果想要實現,那麼我們在add_heading的時候傳入一個空內容
14 _t = title.add_run('\n123')
15 _t.italic = True                #標題追加的內容,可以使其變成斜體
16 _t.bold = True                  #標題加粗,同樣只能對追加的標題內容生效
17 _t.underline = True             #標題加下劃線,同樣只能對追加的標題內容生效
18 #段落
19 paragraph = doc.add_paragraph('這是段落內容')
20 paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER     #段落居中
21 paragraph.style.font.size=Pt(19)    #段落字型大小
22 paragraph.italic = True             #一開始的段落內容無法使其斜體格式生效,如果想要實現,那麼我們在add_paragraph的時候傳入一個空內容
23 _p = paragraph.add_run('\n456')
24 _p.italic = True                    #段落追加的內容,可以使其變成斜體
25 _p.bold = True                      #段落加粗,同樣只能對追加的段落內容生效
26 _p.underline = True                 #段落加下劃線,同樣只能對追加的段落內容生效
27 
28 #通過dir(段落)和dir(標題)可以看到更多的樣式方法
29 
30 #儲存doc物件 word文件
31 doc.save('test.docx')

2.3.3.圖片的居中

 1 # coding:utf-8
 2 
 3 from docx import Document
 4 from docx.shared import Inches
 5 from docx.enum.text import WD_ALIGN_PARAGRAPH
 6 
 7 doc = Document()
 8 
 9 #圖片
10 p1 = doc.add_paragraph()
11 p1.alignment = WD_ALIGN_PARAGRAPH.RIGHT
12 _p1 = p1.add_run()
13 image_obj=_p1.add_picture('logo2020.png',width=Inches(3),height=Inches(2))
14 
15 #儲存doc物件 word文件
16 doc.save('test.docx')
17 
18 print(dir(WD_ALIGN_PARAGRAPH))
19 '''
20 位置有哪些格式:
21 ['CENTER', 'DISTRIBUTE', 'JUSTIFY', 'JUSTIFY_HI', 'JUSTIFY_LOW', 'JUSTIFY_MED', 'LEFT', 'RIGHT', 
22 'THAI_JUSTIFY', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__docs_rst__', 
23 '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__l'
24 '''

2.3.4.表格的樣式定義

  1 # coding:utf-8
  2 
  3 from docx import Document
  4 from docx.enum.style import WD_STYLE_TYPE
  5 
  6 doc = Document()
  7 
  8 for i in doc.styles:
  9     if i.type == WD_STYLE_TYPE.TABLE:
 10         print(i.name)       #列印所有的表格樣式
 11 '''
 12 Normal Table
 13 Table Grid
 14 Light Shading
 15 Light Shading Accent 1
 16 Light Shading Accent 2
 17 Light Shading Accent 3
 18 Light Shading Accent 4
 19 Light Shading Accent 5
 20 Light Shading Accent 6
 21 Light List
 22 Light List Accent 1
 23 Light List Accent 2
 24 Light List Accent 3
 25 Light List Accent 4
 26 Light List Accent 5
 27 Light List Accent 6
 28 Light Grid
 29 Light Grid Accent 1
 30 Light Grid Accent 2
 31 Light Grid Accent 3
 32 Light Grid Accent 4
 33 Light Grid Accent 5
 34 Light Grid Accent 6
 35 Medium Shading 1
 36 Medium Shading 1 Accent 1
 37 Medium Shading 1 Accent 2
 38 Medium Shading 1 Accent 3
 39 Medium Shading 1 Accent 4
 40 Medium Shading 1 Accent 5
 41 Medium Shading 1 Accent 6
 42 Medium Shading 2
 43 Medium Shading 2 Accent 1
 44 Medium Shading 2 Accent 2
 45 Medium Shading 2 Accent 3
 46 Medium Shading 2 Accent 4
 47 Medium Shading 2 Accent 5
 48 Medium Shading 2 Accent 6
 49 Medium List 1
 50 Medium List 1 Accent 1
 51 Medium List 1 Accent 2
 52 Medium List 1 Accent 3
 53 Medium List 1 Accent 4
 54 Medium List 1 Accent 5
 55 Medium List 1 Accent 6
 56 Medium List 2
 57 Medium List 2 Accent 1
 58 Medium List 2 Accent 2
 59 Medium List 2 Accent 3
 60 Medium List 2 Accent 4
 61 Medium List 2 Accent 5
 62 Medium List 2 Accent 6
 63 Medium Grid 1
 64 Medium Grid 1 Accent 1
 65 Medium Grid 1 Accent 2
 66 Medium Grid 1 Accent 3
 67 Medium Grid 1 Accent 4
 68 Medium Grid 1 Accent 5
 69 Medium Grid 1 Accent 6
 70 Medium Grid 2
 71 Medium Grid 2 Accent 1
 72 Medium Grid 2 Accent 2
 73 Medium Grid 2 Accent 3
 74 Medium Grid 2 Accent 4
 75 Medium Grid 2 Accent 5
 76 Medium Grid 2 Accent 6
 77 Medium Grid 3
 78 Medium Grid 3 Accent 1
 79 Medium Grid 3 Accent 2
 80 Medium Grid 3 Accent 3
 81 Medium Grid 3 Accent 4
 82 Medium Grid 3 Accent 5
 83 Medium Grid 3 Accent 6
 84 Dark List
 85 Dark List Accent 1
 86 Dark List Accent 2
 87 Dark List Accent 3
 88 Dark List Accent 4
 89 Dark List Accent 5
 90 Dark List Accent 6
 91 Colorful Shading
 92 Colorful Shading Accent 1
 93 Colorful Shading Accent 2
 94 Colorful Shading Accent 3
 95 Colorful Shading Accent 4
 96 Colorful Shading Accent 5
 97 Colorful Shading Accent 6
 98 Colorful List
 99 Colorful List Accent 1
100 Colorful List Accent 2
101 Colorful List Accent 3
102 Colorful List Accent 4
103 Colorful List Accent 5
104 Colorful List Accent 6
105 Colorful Grid
106 Colorful Grid Accent 1
107 Colorful Grid Accent 2
108 Colorful Grid Accent 3
109 Colorful Grid Accent 4
110 Colorful Grid Accent 5
111 Colorful Grid Accent 6
112 '''
113 title = ['name','age','sex']
114 table = doc.add_table(rows=1,cols=3,style='Colorful List Accent 3')
115 
116 title_cells = table.rows[0].cells
117 for i in range(len(title)):
118     title_cells[i].text=title[i]
119 
120 data=[
121     ('zhangsan','10','man'),
122     ('lisi','20','man'),
123     ('xiaofang','18','women')
124 ]
125 
126 for d in data:
127     row_cells = table.add_row().cells
128     for i in range(len(data)):
129         row_cells[i].text = d[i]
130 #儲存doc物件 word文件
131 doc.save('test.docx')

2.3.生成pdf文件

2.3.1.pdf工具包

  • 下載wkhtmltopdf安裝包,並且安裝到電腦上
  • 在pycharm中安裝wkhtmltopdf庫:
    • pip install wkhtmltopdf
  • 在程式碼中指定configuration引數
    • configuration=pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' 或者設定環境變數:為系統變數path加入wkhtmltopdf安裝目錄的bin即可

2.3.2.html轉pdf

2.3.3.網址轉pdf

2.3.4.字串生成pdf

 1 # coding:utf-8
 2 
 3 import pdfkit
 4 
 5 pdfkit.from_url('https://www.baidu.com','test1.pdf',configuration=pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'))
 6 
 7 html='''
 8 <html>
 9 <head>
10 <meta charset="utf-8" />
11 </head>
12 <body>
13     <p>你好</p>
14 </body>
15 </html>
16 '''
17 pdfkit.from_string(html,'test2.pdf',configuration=pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'))

2.3.5.word轉html之pydocx

# coding:utf-8

import pdfkit
from pydocx import PyDocX

html = PyDocX.to_html('簡歷1.docx')
f = open('html1.html','w',encoding='utf-8')
f.write(html)
f.close()

#方法一
pdfkit.from_file('html1.html','test3.pdf',configuration=pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'))
#方法二
pdfkit.from_string(html,'test4.pdf',configuration=pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'))

 

3.Python Excel自動化

3.1.Excel讀取之xlrd

3.1.1.xlrd的安裝

3.1.2.常用函式介紹

 1 # coding:utf-8
 2 
 3 import xlrd
 4 
 5 excel = xlrd.open_workbook('study.xlsx')
 6 
 7 book = excel.sheet_by_name('學生手冊')
 8 print(book)     #<xlrd.sheet.Sheet object at 0x000001F48391D708>
 9 
10 book = excel.sheet_by_index(0)
11 print(book.name)    #列印sheet的名字,學生手冊
12 
13 for i in excel.sheets():    #列印excel所有的sheet
14     print(i.name)           #學生手冊

3.1.3.讀取工作薄內容

 1 # coding:utf-8
 2 
 3 import xlrd
 4 
 5 excel = xlrd.open_workbook('study.xlsx')
 6 
 7 book = excel.sheet_by_name('學生手冊')
 8 print(book)     #<xlrd.sheet.Sheet object at 0x000001F48391D708>
 9 
10 book = excel.sheet_by_index(0)
11 print(book.name)    #列印sheet的名字,學生手冊
12 
13 for i in excel.sheets():    #列印excel所有的sheet
14     print(i.name)           #學生手冊
15 
16 print(book.nrows)   #行數:6行
17 print(book.ncols)   #列數:5列
18 
19 for i in book.get_rows():
20     print(i)    #裡面每一個鍵值對都是一個物件,[text:'姓名', text:'性別', text:'年齡', text:'成績', text:'等級']
21     content = []
22     for j in i:
23         content.append(j.value) 
24     print(content)  #['姓名', '性別', '年齡', '成績', '等級']

3.2.Excel寫之xlsxwriter

xlsxwriter 只能建立新的檔案(覆蓋),不能對檔案進行資料的操作,往後的隨筆到資料分析可以使用pandas很好的完成追加

3.2.1.xlsxwriter的安裝

3.2.2.常用函式介紹

 1 # coding:utf-8
 2 
 3 import xlsxwriter  # pip install xlsxwriter
 4 
 5 excel = xlsxwriter.Workbook('write1.xlsx')
 6 book = excel.add_worksheet('study')
 7 
 8 title = ['姓名', '性別', '年齡', '成績', '等級']
 9 
10 for index,data in enumerate(title):
11     book.write(0,index,data)
12 excel.close()
 1 # coding:utf-8
 2 
 3 import xlsxwriter  # pip install xlsxwriter
 4 import xlrd
 5 
 6 #excel 檔案之間相互複製
 7 def read():
 8     result = []
 9     excel = xlrd.open_workbook('study.xlsx')
10     book = excel.sheet_by_name('學生手冊')
11     for i in book.get_rows():
12         content=[]
13         for j in i:
14             content.append(j.value)
15         result.append(content)
16     return result
17 
18 
19 def write(content):
20     excel = xlsxwriter.Workbook('write1.xlsx')
21     book = excel.add_worksheet('study')
22 
23     for index,data in enumerate(content):
24         for sub_index,sub_data in enumerate(data):
25             book.write(index,sub_index,sub_data)
26     excel.close()
27 
28 if __name__=='__main__':
29     result = read()
30     write(result)

3.2.3.生成圖表

 1 # coding:utf-8
 2 
 3 import xlsxwriter  # pip install xlsxwriter
 4 import xlrd
 5 
 6 #excel 檔案之間相互複製
 7 def read():
 8     result = []
 9     excel = xlrd.open_workbook('study.xlsx')
10     book = excel.sheet_by_name('學生手冊')
11     for i in book.get_rows():
12         content=[]
13         for j in i:
14             content.append(j.value)
15         result.append(content)
16     return result
17 
18 
19 def write(content):
20     excel = xlsxwriter.Workbook('write1.xlsx')
21     book = excel.add_worksheet('study')
22 
23     for index,data in enumerate(content):
24         for sub_index,sub_data in enumerate(data):
25             book.write(index,sub_index,sub_data)
26 
27     book1 = excel.add_worksheet('學生等級')
28     #生成圖表
29     data = [
30         ['優秀', '良好', '', ''],
31         [1100, 2000, 1000, 900]
32     ]
33     book1.write_column('A1',data[0])
34     book1.write_column('B1',data[1])
35 
36     chart = excel.add_chart({'type':'column'})
37     chart.add_series({
38         'categories': '=學生等級!$A1:$A4',
39         'values': '=學生等級!$B1:$B4',
40         'name': '成績佔比'
41     })
42     chart.set_title({'name': '成績佔比圖表'})
43     #將圖表插入到A10位置
44     book1.insert_chart('A10', chart)
45 
46     excel.close()
47 
48 if __name__=='__main__':
49     result = read()
50     write(result)

 

4.PPT自動化

4.1.PPT的建立

4.1.1.python-pptx的安裝

4.1.2.建立空ppt物件

 1 # coding:utf-8
 2 
 3 import pptx  # pip install python-pptx
 4 
 5 p = pptx.Presentation()  # 生成ppt物件
 6 
 7 layout = p.slide_layouts[1]  # 選擇佈局
 8 
 9 # 0 title
10 # 1 title content
11 # 7
12 slide = p.slides.add_slide(layout)
13 
14 p.save('test1.ppt')

4.2.PPT段落的使用

4.2.1.獲取段落

4.2.2.段落新增內容

 1 # coding:utf-8
 2 
 3 import pptx
 4 
 5 p = pptx.Presentation()
 6 layout = p.slide_layouts[1]  # title content
 7 slide = p.slides.add_slide(layout)
 8 
 9 placeholder = slide.placeholders[1] # 0 title 1 content
10 placeholder.text = '歡迎學習ppt製作\n歡迎學習python'
11 
12 
13 
14 title = slide.placeholders[0]
15 title.text = '題目'
16 p.save('test2.ppt')

4.2.3.段落中定義多個段落

 1 # coding:utf-8
 2 
 3 import pptx
 4 from pptx.util import Pt
 5 from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
 6 
 7 p = pptx.Presentation()
 8 layout = p.slide_layouts[1]  # title content
 9 slide = p.slides.add_slide(layout)
10 
11 placeholder = slide.placeholders[1] # 0 title 1 content
12 
13 
14 title = slide.placeholders[0]
15 title.text = '題目'
16 
17 paragraph1 = placeholder.text_frame.add_paragraph()
18 paragraph1.text = '歡迎學習ppt製作'
19 paragraph1.bold = True
20 paragraph1.font.italic = True
21 paragraph1.font.size = Pt(16)
22 paragraph1.font.underline = True
23 paragraph1.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
24 
25 
26 paragraph2 = placeholder.text_frame.add_paragraph()
27 paragraph2.text = '歡迎學習python'
28 paragraph2.font.size = Pt(32)
29 paragraph2.alignment = PP_PARAGRAPH_ALIGNMENT.RIGHT
30 
31 p.save('test2.ppt')

4.2.4.自定義段落

 1 # coding:utf-8
 2 
 3 import pptx
 4 from pptx.util import Pt, Inches
 5 from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
 6 from pptx.dml.color import RGBColor
 7 
 8 p = pptx.Presentation()
 9 layout = p.slide_layouts[6]  # 只有一個title,沒有段落
10 slide = p.slides.add_slide(layout)
11 left = top = width = height = Inches(5)
12 box = slide.shapes.add_textbox(left, top, width, height)
13 para = box.text_frame.add_paragraph()
14 
15 para.text = 'this is a para test'
16 para.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
17 para.font.size = Pt(32)
18 para.font.color.rgb = RGBColor(255, 255, 0)
19 para.font.name = '微軟雅黑'
20 
21 p.save('test2.ppt')

4.3.PPT插入表格

 1 # coding:utf-8
 2 
 3 import pptx
 4 from pptx.util import Inches
 5 
 6 p = pptx.Presentation()
 7 layout = p.slide_layouts[1]
 8 slide = p.slides.add_slide(layout)
 9 
10 rows = 10
11 cols = 2
12 
13 left = top = Inches(2)
14 width = Inches(6.0)
15 height = Inches(1.0)
16 
17 table = slide.shapes.add_table(rows, cols, left, top, width, height).table
18 
19 for index, _ in enumerate(range(rows)):
20     for sub_index in range(cols):
21         table.cell(index, sub_index).text = '%s:%s' % (index, sub_index)
22 
23 p.save('test2.ppt')

4.4.PPT插入圖片

 1 # coding:utf-8
 2 
 3 import pptx
 4 from pptx.util import Inches
 5 
 6 p = pptx.Presentation()
 7 layout = p.slide_layouts[6]
 8 slide = p.slides.add_slide(layout)
 9 
10 image = slide.shapes.add_picture(
11     image_file='logo2020.png',
12     left=Inches(1),
13     top=Inches(1),
14     width=Inches(6),
15     height=Inches(4)
16 )
17 
18 p.save('test2.ppt')

4.5.讀取PPT

 1 # coding:utf-8
 2 
 3 import pptx
 4 
 5 p = pptx.Presentation('test2.ppt')
 6 for slide in p.slides:
 7     for shape in slide.shapes:
 8         if shape.has_text_frame:
 9             print(shape.text_frame.text)
10         if shape.has_table:
11             for cell in shape.table.iter_cells():
12                 print(cell.text)

 

5.Python郵件自動化

5.1.傳送郵件

 

 1 # coding:utf-8
 2 
 3 #傳送qq郵件
 4 import smtplib
 5 
 6 from email.mime.text import MIMEText
 7 from email.header import Header
 8 
 9 
10 #第三方的smtp
11 mail_host = 'smtp.qq.com'       #smtp伺服器地址
12 mail_user = '34503'         #使用者名稱
13 mail_pass = 'cqsahmvsf假的看自己的'  #smtp 開通, 授權碼
14 
15 sender = '[email protected]'     #傳送人
16 receivers = ['[email protected]'] #接收者郵箱
17 
18 mesage = MIMEText('這是一個測試','plain','utf-8')     #定義郵件傳送內容的物件,編碼格式為utf-8,郵件型別為文字型別的plain普通郵件,郵件內容為:這是一個測試
19 
20 mesage['Form'] = Header(sender)     #郵件傳送內容物件Form屬性:是誰傳送的
21 mesage['Subject'] = Header('python指令碼測試','utf-8')    #郵件傳送內容物件Subject屬性:郵件標題
22 
23 smtpobj = smtplib.SMTP()    #建立smtp協議物件
24 smtpobj.connect(mail_host,25)   #協議連結到服務地址,服務地址埠
25 smtpobj.login(mail_user,mail_pass)  #登入
26 smtpobj.sendmail(sender,receivers,mesage.as_string())   #傳送,傳送者,接收者,郵件物件通過as_string進行加密

5.2.傳送html郵件

 1 # coding:utf-8
 2 
 3 import smtplib
 4 
 5 from email.mime.text import MIMEText
 6 from email.header import Header
 7 
 8 
 9 #第三方的smtp
10 mail_host = 'smtp.qq.com'       #smtp伺服器地址
11 mail_user = '34503'         #使用者名稱
12 mail_pass = 'cqsahmvsfl'  #smtp 開通, 授權碼
13 
14 sender = '[email protected]'     #傳送人
15 receivers = ['g[email protected]'] #接收者郵箱
16 
17 mesage = MIMEText('<p style="color:red;">這是二個測試</p>', 'html', 'utf-8')     #定義郵件傳送內容的物件,編碼格式為utf-8,郵件型別為html型別,郵件內容為:這是二個測試
18 
19 mesage['Form'] = Header(sender)     #郵件傳送內容物件Form屬性:是誰傳送的
20 mesage['Subject'] = Header('python指令碼測試','utf-8')    #郵件傳送內容物件Subject屬性:郵件標題
21 
22 smtpobj = smtplib.SMTP()    #建立smtp協議物件
23 smtpobj.connect(mail_host,25)   #協議連結到服務地址,服務地址埠
24 smtpobj.login(mail_user,mail_pass)  #登入
25 smtpobj.sendmail(sender,receivers,mesage.as_string())   #傳送,傳送者,接收者,郵件物件通過as_string進行加密

5.3.傳送帶附件的郵件

 1 # coding:utf-8
 2 
 3 import smtplib
 4 
 5 from email.mime.text import MIMEText
 6 from email.header import Header
 7 from email.mime.multipart import MIMEMultipart
 8 
 9 #第三方的smtp
10 mail_host = 'smtp.qq.com'       #smtp伺服器地址
11 mail_user = '34503'         #使用者名稱
12 mail_pass = 'cqsahmvsfl'  #smtp 開通, 授權碼
13 
14 sender = '[email protected]'     #傳送人
15 receivers = ['[email protected]'] #接收者郵箱
16 
17 message = MIMEMultipart()
18 message['Form'] = Header(sender)     #郵件傳送內容物件Form屬性:是誰傳送的
19 message['Subject'] = Header('python指令碼測試','utf-8')    #郵件傳送內容物件Subject屬性:郵件標題
20 
21 attr = MIMEText(open('send.py','rb').read(),'base64','utf-8')
22 attr['Content-Type'] = 'application/octet-stream'
23 attr['Content-Disposition'] = 'attachment;filename="send.py"'
24 
25 message.attach(attr)
26 message.attach(MIMEText('這是一個帶附件的郵件', 'plain', 'utf-8'))
27 
28 smtpobj = smtplib.SMTP()    #建立smtp協議物件
29 smtpobj.connect(mail_host,25)   #協議連結到服務地址,服務地址埠
30 smtpobj.login(mail_user,mail_pass)  #登入
31 smtpobj.sendmail(sender,receivers,message.as_string())   #傳送,傳送者,接收者,郵件物件通過as_string進行加密

5.4.定時傳送郵件

 

 

 

 1 # coding:utf-8
 2 
 3 import smtplib
 4 import schedule
 5 
 6 from email.mime.text import MIMEText
 7 from email.header import Header
 8 from email.mime.multipart import MIMEMultipart
 9 from time import sleep
10 #第三方的smtp
11 mail_host = 'smtp.qq.com'       #smtp伺服器地址
12 mail_user = '34503'         #使用者名稱
13 mail_pass = 'cqsahmvsfl'  #smtp 開通, 授權碼
14 
15 sender = '[email protected]'     #傳送人
16 receivers = ['[email protected]'] #接收者郵箱
17 
18 message = MIMEMultipart()
19 message['Form'] = Header(sender)     #郵件傳送內容物件Form屬性:是誰傳送的
20 message['Subject'] = Header('python指令碼測試','utf-8')    #郵件傳送內容物件Subject屬性:郵件標題
21 
22 attr = MIMEText(open('send.py','rb').read(),'base64','utf-8')
23 attr['Content-Type'] = 'application/octet-stream'
24 attr['Content-Disposition'] = 'attachment;filename="send.py"'
25 
26 message.attach(attr)
27 message.attach(MIMEText('這是一個帶附件的郵件', 'plain', 'utf-8'))
28 
29 def send():
30     try:
31         smtpobj = smtplib.SMTP()    #建立smtp協議物件
32         smtpobj.connect(mail_host,25)   #協議連結到服務地址,服務地址埠
33         smtpobj.login(mail_user,mail_pass)  #登入
34         smtpobj.sendmail(sender,receivers,message.as_string())   #傳送,傳送者,接收者,郵件物件通過as_string進行加密
35     except smtplib.SMTPException as e:
36         print('error: %s' %e)
37 
38 
39 if __name__=='__main__':
40     schedule.every(10).seconds.do(send)
41 
42     while 1:
43         schedule.run_pending()
44         sleep(1)