﻿#encoding=utf-8
import win32com.client
import os
import sys
import win32com.gen_py.MSO as MSO # contains constants refering to Microsoft Office Objects
import win32com.gen_py.MSPPT as MSPPT # contains constants refering to Microsoft Office Power Point Objects
import time
import constDefine
from oss_api import *
from oss_xml_handler import *
HOST=constDefine.returnHOST()
ACCESS_ID = constDefine.returnACCESSID()
SECRET_ACCESS_KEY = constDefine.returnSAKEY()
def change(dpath):
    oss = OssAPI(HOST, ACCESS_ID, SECRET_ACCESS_KEY)
    content_type = "images/png"
    headers={}
    bucketname = "zijing"
    
    g = globals() # a dictonary of global vlaues, that will be the constants of the two previous imports
    for c in dir(MSO.constants): g[c] = getattr(MSO.constants, c) # globally define these
    for c in dir(MSPPT.constants): g[c] = getattr(MSPPT.constants, c)

    # Open PowerPoint
    Application = win32com.client.Dispatch("PowerPoint.Application")
    Application.Visible = True

    # Create new presentation
    #myPresentation = Application.Presentations.Add()

    #Open presentation 
    #myPresentation = Application.Presentations.Open(filepath)
    filepath = "C:\\saveppt\\"+dpath
    print filepath
    myPresentation = Application.Presentations.Open(filepath)
    wholename = filepath.split("\\")[-1]
    index = wholename.find('.')
    filename = wholename[0:index]
    print filename
    #filename = "iHappen"
    savepath = "C:\\pptsave2"
    savepath = savepath + "\\"+filename
    myPresentation.SaveAs(savepath,ppSaveAsPNG)
    myPresentation.Close()
    Application.Quit()
    i=0
    opath = "C:\\pptsave2\\"+filename
    dirpath = dpath.replace('/','\\') 
    #oname = "SlideSharer.pptx"
    #oss.put_object_from_file(bucketname,dpath,os.path.join(opath,oname),'application/octet-stream',headers)
    #oss.put_object_from_file(bucketname,dpath,opath,'application/octet-stream',headers)
    """for path,dirs,files in os.walk(savepath,'topdown'):
        #print files
        for name in files:
            #newname = filename+"/"+filename+str(i)+".jpg"
            os.rename("C:\\pptsave\\"+filename+"\\"+name,"C:\\pptsave\\"+filename+"\\"+"slide"+str(i))
            """
    for path,dirs,files in os.walk(savepath,'topdown'):
        #print files
        for name in files:
            newname = filename+"/"+filename+str(i)+".png"        
            res = oss.put_object_from_file(bucketname,newname,os.path.join(opath,name),'application/octet-stream',headers)
            #oss.put_object_from_file(bucketname,dirpath,opath,content_type ,headers)
            i = i+1
            if(res.status/100)==2:
                print "put_object_from_fp OK"
            else:
                print "put_object_from_fp ERROR"
    #os.rmdir(savepath)

if __name__== "__main__":
    #change("E:\\pythonText\\test\\iHappen.pptx")
    
    change(sys.argv[1])
    path = str(sys.argv[1])
    print sys.argv[1]
    #change(path)
    #change(sys.argv[1])
    #print "hello"