使用Python调整Powerpoint中的图像大小

问题描述 投票:0回答:1

我正在复制 Excel 图表,然后将其粘贴到 Power Point 幻灯片中。粘贴图表后,我想调整以图像格式粘贴的图表的大小。请帮忙

import win32com.client 

# Grab the Active Instance of Excel.
ExcelApp = win32com.client.Dispatch("Excel.Application")
ExcelApp.Visible = True

# Grab the workbook with the charts.
xlWorkbook = ExcelApp.Workbooks.Open(r'C:\Users\anavs\Anavarathan\Nokia\NAM\OneWeb\Activity\Python\Chart\NDC_wan_Oct24.xlsx')

# Create a new instance of PowerPoint and make sure it's visible.
PPTApp = win32com.client.gencache.EnsureDispatch("PowerPoint.Application")
PPTApp.Visible = True

# Add a presentation to the PowerPoint Application, returns a Presentation Object.
PPTPresentation = PPTApp.Presentations.Add()

# Loop through each Worksheet.
for xlWorksheet in xlWorkbook.Worksheets:

    # Grab the ChartObjects Collection for each sheet.
    xlCharts = xlWorksheet.ChartObjects()

    # Loop through each Chart in the ChartObjects Collection.
    for index, xlChart in enumerate(xlCharts):
        # Each chart needs to be on it's own slide, so at this point create a new slide.
        PPTSlide = PPTPresentation.Slides.Add(Index=index + 1, Layout=12)  # 12 is a blank layout
        
        # Display something to the user.
        print('Exporting Chart {} from Worksheet {}'.format(xlChart.Name, xlWorksheet.Name))

        # Copy the chart.
        xlChart.Copy()

        # Paste the Object to the Slide
        PPTSlide.Shapes.PasteSpecial(DataType=1)

    # Save the presentation.
PPTPresentation.SaveAs(r"C:\Users\anavs\Anavarathan\Nokia\NAM\OneWeb\Activity\Python\Chart\\output")

我在下面尝试过,但不起作用

PPTSlide.Shapes(1).width = 20
PPTSlide.Shapes.items(1).width = 20
PPTSlide.Shapes.width = 20
python excel powerpoint
1个回答
0
投票

以下内容应该允许您操作图表图像;
请注意 Win32 常量的额外导入

import win32com.client 
from win32com.client import constants

# Grab the Active Instance of Excel.
ExcelApp = win32com.client.Dispatch("Excel.Application")
ExcelApp.Visible = True

# Grab the workbook with the charts.
xlWorkbook = ExcelApp.Workbooks.Open(r'C:\Users\anavs\Anavarathan\Nokia\NAM\OneWeb\Activity\Python\Chart\NDC_wan_Oct24.xlsx')

# Create a new instance of PowerPoint and make sure it's visible.
PPTApp = win32com.client.gencache.EnsureDispatch("PowerPoint.Application")
PPTApp.Visible = True

# Add a presentation to the PowerPoint Application, returns a Presentation Object.
PPTPresentation = PPTApp.Presentations.Add()

# Loop through each Worksheet.
for xlWorksheet in xlWorkbook.Worksheets:

    # Grab the ChartObjects Collection for each sheet.
    xlCharts = xlWorksheet.ChartObjects()

    # Loop through each Chart in the ChartObjects Collection.
    for index, xlChart in enumerate(xlCharts):
        # Each chart needs to be on it's own slide, so at this point create a new slide.
        PPTSlide = PPTPresentation.Slides.Add(Index=index + 1, Layout=12)  # 12 is a blank layout
        
        # Display something to the user.
        print('Exporting Chart {} from Worksheet {}'.format(xlChart.Name, xlWorksheet.Name))

        # Copy the chart.
        xlChart.Copy()

        # Paste the Object to the Slide
        # PPTSlide.Shapes.PasteSpecial(DataType=1)

        ### Paste chart and resize either as a scaled from original or specific values
        img = PPTSlide.Shapes.PasteSpecial(constants.ppPasteShape)
        ### Scaled
        # img.ScaleWidth(1.5, 0)
        # img.ScaleHeight(1.5, 0)
        ### Set Specific Size
        img.Width = 150
        img.Height = 150
        ### Also set position of the image in the slide
        img.Left = 100
        img.Top = 100

    # Save the presentation.
PPTPresentation.SaveAs(r"C:\Users\anavs\Anavarathan\Nokia\NAM\OneWeb\Activity\Python\Chart\\output")
© www.soinside.com 2019 - 2024. All rights reserved.