Import win32com
App_type = ' Excel.Application '
Xlblack,xlred,xlgray,xlblue = 1,3,15,41
Xlbreakfull = 1
#初始化应用程序
XLS = Win32com.client.Dispatch (app_type)
Xls. Visible = True
Book = xls. Workbooks.Add ()
Sheet = Book. Worksheets (1)
#插入标题
Row_per_page,col_per_row = 10, 10
Row_index,col_index = 1
Title_range = sheet. Range (sheet. Cells (row_index,col_index), sheet. Cells (Row_index, Col_per_row))
Title_range. MergeCells = True
Title_range. Font.bold,title_range. Interior.ColorIndex = True,xlgray
Title_range. Value = ' Hello,word '
Row_index + = 1
The sequence of #插入内容 10*10
For row in range (0, 10):
Col_index = 1
For Col in range (0, Col_per_row):
Cell_range = sheet. Cells (Row_index, Col_index)
Cell_range. Font.color,cell_range. Value = Xlblue,str (Row)
Row_index + = 1
#插入分页符
Right_bottom_range = sheet. Cells (Row_index, col_per_row+1)
Right_bottom_range. PageBreak = Xlbreakfull
#插入图片
Col_index = 1
Lt_range = sheet. Cells (Row_index, Col_index)
Graph_width = sheet. Range (sheet. Cells (row_index,1), sheet. Cells (Row_index, Col_per_row)). Width
Graph_height = sheet. Range (sheet. CellS (row_index,1), sheet. Cells (Row_index+row_per_page, 1)). Height
Sheet. Shapes.addpicture (' c:\\test.jpg ', False, True, Lt_range. Left, Lt_range. Top, Graph_width, Graph_height)
Python+win32--com Implementing Excel Automation