gSlide.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from __future__ import print_function
  2. import os.path
  3. from googleapiclient.discovery import build
  4. from google_auth_oauthlib.flow import InstalledAppFlow
  5. from google.auth.transport.requests import Request
  6. from google.oauth2.credentials import Credentials
  7. from google.oauth2 import service_account
  8. import argparse
  9. import requests
  10. import pprint
  11. import json
  12. import calendar
  13. import time
  14. import os
  15. import shutil
  16. from distutils.util import strtobool
  17. SCOPES = ['https://www.googleapis.com/auth/presentations.readonly',
  18. 'https://www.googleapis.com/auth/drive.metadata.readonly']
  19. dir_sound = 'mp3_track/'
  20. dir_photo = 'photo/'
  21. dir_text = 'text_file/'
  22. dir_video = 'video_material/'
  23. dir_title = 'title/'
  24. dir_subtitle = 'subtitle/'
  25. dir_anchor = 'anchor_raw/'
  26. tmp_video_dir = 'tmp_video/'
  27. video_sub_folder = 'ai_anchor_video/'
  28. def parse_url(url):
  29. #https://docs.google.com/presentation/d/17jJ3OZWh8WorFcolB_LiTa7xQ3R-xrmFlqJ_EyCj06M/edit#slide=id.p
  30. return url.split('/')[5]
  31. def parse_slide_url(slide_url,eng):
  32. PRESENTATION_ID = parse_url(slide_url)
  33. credentials = service_account.Credentials.from_service_account_file('spread2.json')
  34. scoped_credentials = credentials.with_scopes(SCOPES)
  35. creds = credentials
  36. notes_list=[]
  37. sub_title_list=[]
  38. img_list=[]
  39. service = build('slides', 'v1', credentials=creds)
  40. # Call the Slides API
  41. presentation = service.presentations().get(
  42. presentationId=PRESENTATION_ID).execute()
  43. slides = presentation.get('slides')
  44. for i, slide in enumerate(slides):
  45. # Check if the notes exists
  46. print(slide['slideProperties']['notesPage']['pageElements'][1]['shape'].keys())
  47. notes=''
  48. if 'text' in slide['slideProperties']['notesPage']['pageElements'][1]['shape'].keys():
  49. notes = slide['slideProperties']['notesPage']['pageElements'][1]['shape']['text']['textElements'][1]['textRun']['content']
  50. else:
  51. notes = slide['slideProperties']['notesPage']['pageElements'][0]['shape']['text']['textElements'][1]['textRun']['content']
  52. if '[sub_title]' in notes:
  53. sub_title = notes.split('[sub_title]')[1].strip()
  54. sub_title_list.append(sub_title)
  55. notes = notes.split('[sub_title]')[0].strip()
  56. notes_list.append(notes)
  57. # Convert the content of the presentation to png
  58. thumbnail = service.presentations().pages().getThumbnail(presentationId=PRESENTATION_ID, pageObjectId=slide['objectId']).execute()
  59. img_list.append(thumbnail['contentUrl'])
  60. # data
  61. slide_content = { "name": presentation['title'], "text_content": notes_list, "image_urls": img_list, "avatar": "7", "client_id": calendar.timegm(time.gmtime()) }
  62. if eng:
  63. slide_content['sub_titles'] = sub_title_list
  64. return slide_content['name'],slide_content['text_content'],slide_content['image_urls']
  65. def parse_slide_url(fileanme,img_upload_folder,img_url,eng):
  66. notes_list=[]
  67. sub_title_list=[]
  68. img_list=[]
  69. prs = Presentation(fileanme)
  70. for slide in prs.slides:
  71. notes_slide = slide.notes_slide
  72. text_frame = notes_slide.notes_text_frame
  73. print(text_frame.text)
  74. shapes = slide.shapes
  75. notes_list.append(text_frame.text)
  76. for s in shapes:
  77. img_name = str(time.time()).replace('.','')
  78. image = s.image
  79. image_bytes = image.blob
  80. # ---make up a name for the file, e.g. 'image.jpg'---
  81. image = Image.open(image_bytes)
  82. image= image.convert("RGB")
  83. image.save(img_upload_folder+img_name+'.jpg')
  84. img_list.append(img_url+img_name+'.jpg')
  85. #image_filename = ''
  86. #with open(image_filename, 'wb') as f:
  87. # f.write(image_bytes)
  88. return filename, notes_list, img_list