CODE‎ > ‎Python‎ > ‎

excel插入圖片

 #!/usr/bin/env python
 #coding=utf-8
 #需要先修改Pics的位置。生成的excel位于c:\test.xlsx.(word2007,如果要2003的,把filename后缀改成xls即可)
 from win32com.client import Dispatch
 import win32com.client
 import os
 
 def creatExcel(filename):
     """creat a new excel to edit"""
     excel = win32com.client.Dispatch("Excel.Application")        
     d1 = excel.Workbooks.Add()      
     sheet = d1.Sheets(1)   
     d1.SaveAs(filename)   
     excel.Quit()
     
 
 class easyExcel:
       """A utility to make it easier to get at Excel.    Remembering
       to save the data is your problem, as is    error handling.
       Operates on one workbook at a time."""
 
       def __init__(self, filename=None):
           self.xlApp = win32com.client.Dispatch('Excel.Application')
           if filename:
               self.filename = filename
               self.xlBook = self.xlApp.Workbooks.Open(filename)
           else:
               self.xlBook = self.xlApp.Workbooks.Add()
               self.filename = ''  
     
       def save(self, newfilename=None):
           if newfilename:
               self.filename = newfilename
               self.xlBook.SaveAs(newfilename)
           else:
               self.xlBook.Save()    
 
       def close(self):
           self.xlBook.Close(SaveChanges=0)
           del self.xlApp
 
       def getCell(self, sheet, row, col):
           "Get value of one cell"
           sht = self.xlBook.Worksheets(sheet)
           return sht.Cells(row, col).Value
 
       def setCell(self, sheet, row, col, value):
           "set value of one cell"
           sht = self.xlBook.Worksheets(sheet)
           sht.Cells(row, col).Value = value
 
       def getRange(self, sheet, row1, col1, row2, col2):
           "return a 2d array (i.e. tuple of tuples)"
           sht = self.xlBook.Worksheets(sheet)
           return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value
 
       def addPicture(self, sheet, pictureName, Left, Top, Width, Height):
           "Insert a picture in sheet"
           sht = self.xlBook.Worksheets(sheet)
           sht.Shapes.AddPicture(pictureName, 11, Left, Top, Width, Height)
   
       def cpSheet(self, before):
           "copy sheet"
           shts = self.xlBook.Worksheets
           shts(1).Copy(None,shts(1))
 
 if __name__ == "__main__":
       dic = 'c:\\temp'
       i = 0
       j = 0
       list = os.listdir(dic)
       dict = {0:list[0],1:list[11],2:list[22],3:list[33],4:list[39],5:list[40],6:list[41],7:list[42],8:list[43],9:list[1],10:list[2],\
                11:list[3],12:list[4],13:list[5],14:list[6],15:list[7],16:list[8],17:list[9],18:list[10],19:list[12],20:list[13],21:list[14],\
               22:list[15],23:list[16],24:list[17],25:list[18],26:list[19],27:list[20],28:list[21],29:list[23],30:list[24],31:list[25],32:list[26],33:list[27],34:list[28],\
               35:list[29],36:list[30],37:list[31],38:list[32],39:list[34],40:list[35],41:list[36],42:list[37],43:list[38]}
       filename = 'c:\\test.xlsx'
       creatExcel(filename)
       xls = easyExcel(filename)
       for odd in range(0,44,2):
         PNFILE_odd = dic+'\\'+dict[odd]
         xls.addPicture('Sheet1', PNFILE_odd, 0,i*177,446,177)
         i += 1
       for even in range(1,45,2):
         PNFILE_even = dic+'\\'+dict[even]
         xls.addPicture('Sheet1', PNFILE_even, 460,j*177,446,177)
         j += 1
       xls.cpSheet('Sheet1')
       xls.save()
       xls.close()

Comments