Chapter 08 實戰 :Facebook 貼文 按讚與照片下載 Facebook Python Facebook Facebook Graph API Explorer Facebook Python facebook-sdk Facebook API Facebook Python
Python 8.2.4 Facebook put_wall_post(message,attachment [,profile_id]) put_wall_post(message,attachment [,profile_id]) message message attachment profile_id id Python publish_actions Facebook (<fbpost.py>) 1 import facebook 2 3 token="eaacedeose0cbag4kwxryesx3r5odadee3e " 4 graph = facebook.graphapi(access_token=token,version='2.7') 5 6 message = " Python fb!" 7 attachment = { 8 'name': ' ', 9 'link': 'http://www.e-happy.com.tw/indexbookshow.asp?bid=278', 10 'caption': ' ', 8-14
Facebook 11 'description': ' ', 12 'picture': 'http://www.e-happy.com.tw/images/bookimg/10-056-s.jpg' 13 } 14 15 graph.put_wall_post(message=message, attachment=attachment) 16 print("!") 8.2.5 Facebook put_like(object_id ) put_like(object_id ) id Python (<fbputlike.py>) 1 import facebook 2 3 token="eaacedeose0cbag4kwxryesx3r5odadee3eizb7... " 4 graph = facebook.graphapi(access_token=token,version='2.7') 5 6 pages = graph.get_connections(id='me', connection_name='posts') 7 posts = pages['data'] 8 9 for p in posts: 10 graph.put_like(p['id']) 11 print (p['id'],"!") connection_name='feed' 8-15
Chapter 10 實戰 :Word 文件處理 Office Word Python Win32com Microsoft Office Python Win32com Win32com Microsoft Office Microsoft Office Win32com Word Word ( ) Word Python
Python Word Word 3 4 ch10\tabledocx1.py 7 doc = word.documents.open(cpath + "\\media\\clipgraph.docx") 8 data = [ [" ", " ", " ", " "], ["A8", "5.0 ", " ", "8000"], \ 9 ["A10", "5.5 ", " ", "22000"] ] 10 paragraphs = doc.paragraphs 11 range1 = paragraphs(4).range 12 table = doc.tables.add(range1, 3, 4) 13 for i in range(1,table.rows.count+1): 14 for j in range(1,table.columns.count+1): 15 table.cell(i,j).range.text = data[i-1][j-1] 16 table.cell(2,3).range.font.color = 0x0000FF 17 #doc.close() 18 #word.quit() 程式說明 8-9 3x4 10-11 4 4 12 3 4 13-14 1 range 1 1 15 1 0 data[i-1][j-1] 16 2 3 10-10
實戰 Word 文件處理 10 10.1.5 加入圖片 在 Word 文件加入圖片的語法為 範圍變數.InlineShapes.AddPicture( 檔案路徑, 連結, 儲存 ) 連結 是布林值 True 表示連結到原始圖片檔 False 表示不連結到圖片檔 儲存 是布林值 True 表示將原始圖片檔存於 Word 檔中 False 表示 Word 檔中不儲存圖片檔 例如在 <clipgraph.docx> 段落 4 插入 <cell.jpg> 圖片 並將圖片檔儲存於 Word 檔內 (<imagedocx1.py>) 略 doc = word.documents.open(cpath + "\\media\\clipgraph.docx") paragraphs = doc.paragraphs range1 = paragraphs(4).range range1.inlineshapes.addpicture(cpath + "\\media\\cell.jpg", False, True) 執行結果為 插入圖片 10.1.6 取代文字 Win32com 套件提供自動取代文件中指定文字的功能 通常使用取代文字前會先 清除搜尋文字及取代文字的格式 以免因格式影響取代效果 語法為 Word 應用變數.Selection.Find.ClearFormatting() Word 應用變數.Selection.Find.Replacement.ClearFormatting() 10-11
Python Word Word.Selection.Find.Execute(, 1, 2, 3, 4, 5,,,,, ) 比對 1True False 比對 2True False 比對 3True False 比對 4True Like False Like 比對 5True False ( ) 方向 True False 動作 constants.wdfindask constants.wdfindcontinue constants.wdfindstop 格式 True False 取代次數 constants.wdreplacenone constants.wdreplaceone constants.wdreplaceall Word Word method 10-12
Chapter 12 實戰 : 臉部辨識及驗證碼圖片破解 OpenCV OpenCV OpenCV CascadeClassifier Python Tesseract OCR Python
實戰 臉部辨識及驗證碼圖片破解 12 12.2.3 實戰 驗證碼破解 應用程式總覽 程式執行後會顯示驗證碼圖形 按任意鍵後會在命令視窗顯示辨識結果 (<format. py>) 辨識結果 應用程式內容 程式碼 ch02\format.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import cv2, subprocess img = cv2.imread("media\\bank.jpg") # 讀圖 cv2.namedwindow("image") cv2.imshow("image", img) # 顯示圖形 cv2.waitkey (0) cv2.destroywindow("image") gray = cv2.cvtcolor(img, cv2.color_bgr2gray) # 轉為灰階 _, inv = cv2.threshold(gray, 150, 255, cv2.thresh_binary_inv) # 轉為反相黑白 for i in range(len(inv)): #i 為每一列 for j in range(len(inv[i])): #j 為每一行 if inv[i][j] == 255: # 顏色為白色 count = 0 for k in range(-2, 3): for l in range(-2, 3): try: if inv[i + k][j + l] == 255: count += 1 except IndexError: pass if count <= 6: # 週圍少於等於 6 個白點 inv[i][j] = 0 # 將白點去除 dilation = cv2.dilate(inv, (8,8), iterations=1) # 圖形加粗 cv2.imwrite("media\\bank_t.jpg", dilation) # 存檔 12-25
Chapter 16 實戰 : PyGame 遊戲開發 PyGame Python Python PyGame Python Python
PyGame 16.3 16.3.1 x 60 16-21
Python 16.3.2 16.2.4 ch16\brickgame.py 1 import pygame, random, math, time 2 3 class Ball(pygame.sprite.Sprite): # 4 dx = 0 #x 5 dy = 0 #y 6 x = 0 # x 7 y = 0 # y 8 direction = 0 # 9 speed = 0 # 10 11 def init (self, sp, srx, sry, radium, color): 12 pygame.sprite.sprite. init (self) 13 self.speed = sp 14 self.x = srx 15 self.y = sry 16 self.image = pygame.surface([radium*2, radium*2]) # 17 self.image.fill((255,255,255)) 18 pygame.draw.circle(self.image, color, (radium,radium), radium, 0) 19 self.rect = self.image.get_rect() # 20 self.rect.center = (srx,sry) # 21 self.direction = random.randint(40,70) # 22 23 def update(self): # 24 radian = math.radians(self.direction) # 25 self.dx = self.speed * math.cos(radian) # 26 self.dy = -self.speed * math.sin(radian) # 27 self.x += self.dx # 28 self.y += self.dy 29 self.rect.x = self.x # 30 self.rect.y = self.y 31 if(self.rect.left <= 0 or self.rect.right >= screen.get_width()-10): # 32 self.bouncelr() 33 elif(self.rect.top <= 10): # 34 self.rect.top = 10 35 self.bounceup() 16-22
PyGame 36 if(self.rect.bottom >= screen.get_height()-10): # 37 return True 38 else: 39 return False 40 41 def bounceup(self): # 42 self.direction = 360 - self.direction 43 44 def bouncelr(self): # 45 self.direction = (180 - self.direction) % 360 程式說明 11-21 23-39 24-26 27-30 31-32 33-35 34 y 10 36-39 True False 41-42 = 360 0-30 330 30 0 0 30 3300 330 0 44-45 = 180 0-360 30 150 16-23