gSlide.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. from pptx import Presentation
  18. SCOPES = ['https://www.googleapis.com/auth/presentations.readonly',
  19. 'https://www.googleapis.com/auth/drive.metadata.readonly']
  20. dir_sound = 'mp3_track/'
  21. dir_photo = 'photo/'
  22. dir_text = 'text_file/'
  23. dir_video = 'video_material/'
  24. dir_title = 'title/'
  25. dir_subtitle = 'subtitle/'
  26. dir_anchor = 'anchor_raw/'
  27. tmp_video_dir = 'tmp_video/'
  28. video_sub_folder = 'ai_anchor_video/'
  29. def parse_url(url):
  30. #https://docs.google.com/presentation/d/17jJ3OZWh8WorFcolB_LiTa7xQ3R-xrmFlqJ_EyCj06M/edit#slide=id.p
  31. return url.split('/')[5]
  32. def parse_slide_url(slide_url,eng):
  33. PRESENTATION_ID = parse_url(slide_url)
  34. credentials = service_account.Credentials.from_service_account_file('spread2.json')
  35. scoped_credentials = credentials.with_scopes(SCOPES)
  36. creds = credentials
  37. notes_list=[]
  38. sub_title_list=[]
  39. img_list=[]
  40. service = build('slides', 'v1', credentials=creds)
  41. # Call the Slides API
  42. presentation = service.presentations().get(
  43. presentationId=PRESENTATION_ID).execute()
  44. slides = presentation.get('slides')
  45. for i, slide in enumerate(slides):
  46. # Check if the notes exists
  47. print(slide['slideProperties']['notesPage']['pageElements'][1]['shape'].keys())
  48. notes=''
  49. if 'text' in slide['slideProperties']['notesPage']['pageElements'][1]['shape'].keys():
  50. notes = slide['slideProperties']['notesPage']['pageElements'][1]['shape']['text']['textElements'][1]['textRun']['content']
  51. else:
  52. notes = slide['slideProperties']['notesPage']['pageElements'][0]['shape']['text']['textElements'][1]['textRun']['content']
  53. if '[sub_title]' in notes:
  54. sub_title = notes.split('[sub_title]')[1].strip()
  55. sub_title_list.append(sub_title)
  56. notes = notes.split('[sub_title]')[0].strip()
  57. notes_list.append(notes)
  58. # Convert the content of the presentation to png
  59. thumbnail = service.presentations().pages().getThumbnail(presentationId=PRESENTATION_ID, pageObjectId=slide['objectId']).execute()
  60. img_list.append(thumbnail['contentUrl'])
  61. # data
  62. slide_content = { "name": presentation['title'], "text_content": notes_list, "image_urls": img_list, "avatar": "7", "client_id": calendar.timegm(time.gmtime()) }
  63. if eng:
  64. slide_content['sub_titles'] = sub_title_list
  65. return slide_content['name'],slide_content['text_content'],slide_content['image_urls']
  66. #www.choozmo.com:8168/tmp_pttx/1634388575388378.pptx
  67. #www.choozmo.com:8168/tmp_img/16344598943505244.jpg
  68. def parse_pttx_url(fileanme,img_upload_folder,img_url,eng):
  69. prs = Presentation(fileanme)
  70. filename = filename[:filename.find('#')]
  71. imgurl = []
  72. for slide in prs.slides:
  73. for shape in slide.shapes:
  74. with open('mypic.jpg', 'wb') as f:
  75. f.write(shape.image.blob)
  76. text = []
  77. for page, slide in enumerate(prs.slides):
  78. textNote = slide.notes_slide.notes_text_frame.text
  79. print(textNote)
  80. return filename, imgurl, text