Python PowerPoint 操作:将 Excel 数据表格插入幻灯片并调整位置
///'To adjust the position of the two elements in each slide, you can use the Left and Top properties of the shapes. Here's an example of how you can modify your code to achieve this://n//npython//nppt_app = win32com.client.Dispatch(/'PowerPoint.Application/')//nppt_presentation = ppt_app.Presentations.Add(True)//n//nfor i in range(len(result))://n start_row = result[i][0]//n end_row = result[i][-1]//n//n max_col = len(df.iloc[start_row-1:end_row].columns)//n min_col = 1//n//n ppt_presentation.Slides.Add(i+1, 12)//n ppt_slide = ppt_presentation.Slides(i+1)//n ppt_app.ActiveWindow.View.GotoSlide(i+1)//n//n # Copy and paste table's title//n title = worksheet.Range(worksheet.Cells(start_row, min_col), worksheet.Cells(start_row, min_col)).Address//n worksheet.Range(title).Copy()//n ppt_app.CommandBars.ExecuteMso(/'PasteExcelTableSourceFormatting/')//n shape_title = ppt_slide.Shapes(1)//n shape_title.Left = 0//n shape_title.Top = 0//n//n # Copy and paste table//n range_address = worksheet.Range(worksheet.Cells(start_row+1, min_col), worksheet.Cells(end_row, max_col)).Address//n worksheet.Range(range_address).Copy()//n ppt_app.CommandBars.ExecuteMso(/'PasteExcelTableSourceFormatting/')//n shape_table = ppt_slide.Shapes(2)//n shape_table.Left = 100 # Adjust the left position of the table//n shape_table.Top = 100 # Adjust the top position of the table//n shape_table.Width = ppt_slide.Master.Width # Resize table to fit slide width//n shape_table.Height = ppt_slide.Master.Height - shape_title.Height # Resize table to fit slide height//n//n time.sleep(1)//n//n//nIn this example, I've added two shape variables shape_title and shape_table to represent the title and table shapes, respectively. You can adjust the Left and Top properties of shape_table according to your desired position. Additionally, I've resized the table shape to fit the slide width and height by setting the Width and Height properties accordingly.//n//n///
原文地址: https://www.cveoy.top/t/topic/p84B 著作权归作者所有。请勿转载和采集!