이 예제는 Python을 사용하여 PowerPoint 테이블에 이미지를 삽입하는 방법에 중점을 둡니다. 환경을 설정하는 데 필요한 모든 정보와 Python의 PPTX 테이블에 이미지를 추가하는 작업 예제 코드를 설명합니다. 이 응용 프로그램은 Linux, macOS 또는 Windows와 같은 Python 구성 환경에서 활용할 수 있습니다.
Python을 사용하여 PowerPoint 테이블에 이미지를 삽입하는 단계
- 환경을 .NET을 통해 Python용 Aspose.Slides 사용로 설정하여 테이블 내부에 이미지를 삽입합니다.
- Presentation 클래스의 인스턴스를 사용하여 기본 프레젠테이션을 만든 다음 슬라이드 모음에서 첫 번째 슬라이드에 액세스합니다.
- add_table() 메서드를 사용하여 선택한 슬라이드 내부에 사전 정의된 수의 행과 열이 있는 내부에 표를 삽입합니다.
- 프리젠테이션 이미지 모음에 이미지 삽입
- 테이블 내부의 첫 번째 행과 열에 속하는 셀에 액세스하고 그 안에 이미지를 설정하십시오.
- 테이블 이미지와 함께 PPTX 프레젠테이션을 디스크에 저장
위에서 언급한 단계를 사용하여 Python의 PPTX 테이블에 이미지를 표시하는 방법을 살펴보았습니다. 처음에는 프레젠테이션 슬라이드 컬렉션 내의 첫 번째 슬라이드에 액세스하는 것과 함께 Presentation 클래스의 인스턴스를 사용하여 기본 프레젠테이션이 생성됩니다. add_table() 메서드를 사용하여 미리 정의된 행과 열의 수를 사용하여 새 테이블을 만든 다음 디스크에서 소스 이미지를 로드하고 프레젠테이션 이미지 컬렉션에 삽입합니다. 결국 프레젠테이션을 디스크에 저장하기 전에 테이블 셀 안에 추가된 이미지를 설정합니다.
Python을 사용하여 PowerPoint 테이블에 이미지를 삽입하는 코드
import aspose.pydrawing as draw | |
import aspose.slides as slides | |
#Path to the license and image file directory | |
filepath = "Y://Documents//KnowledgeBase//TestData//" | |
# Load the license in your application for creating a table with an image | |
slidesTableLicense = slides.License() | |
slidesTableLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic") | |
#Create a Presentation object to add a table with an image | |
with slides.Presentation() as presTable: | |
# Access the first slide to add the table | |
slideForTable = presTable.slides[0] | |
#Define the rows' heights and columns' widths | |
dblColWidth = [50, 50, 50] | |
dblRowHeight = [50, 30, 32, 30] | |
#Add a table shape to slide | |
tblwithImage = slideForTable.shapes.add_table(100, 50, dblColWidth, dblRowHeight) | |
with open(filepath + "sample.png", "rb") as bin_file: | |
#Read the entire file from the disk at once | |
tblImageData = bin_file.read() | |
#Insert the image insdie the images collection of the presentation | |
imageForSlide = presTable.images.add_image(tblImageData) | |
#Access the first cells inside the first row of the table | |
tableCell = tblwithImage[0,0] | |
#Set the cell fill format to picture | |
tableCell.cell_format.fill_format.fill_type = slides.FillType.PICTURE | |
#Set the picture fill mode | |
tableCell.cell_format.fill_format.picture_fill_format.picture_fill_mode = slides.PictureFillMode.STRETCH | |
#Set the image for the selected cell inside the table | |
tableCell.cell_format.fill_format.picture_fill_format.picture.image = imageForSlide | |
#Save the presentations with table image | |
presTable.save(filepath + "PresWithTableImage.pptx", slides.export.SaveFormat.PPTX) | |
print("Done") | |
위의 예는 매우 간단한 API 인터페이스를 사용하여 Python을 사용하여 슬라이드에 테이블 이미지를 삽입하는 방법을 보여줍니다. cell_format은 fill_type.PICTURE 열거자를 사용하여 셀의 채우기 형식을 이미지로 설정하는 데 사용됩니다. 이 예에서는 테이블 셀에 대한 PNG 이미지를 추가했습니다. 그러나 테이블 셀에 대해 JPEG, BMP, EMF 및 SVG를 비롯한 다른 이미지 유형을 추가할 수도 있습니다.
이 항목에서는 Python을 사용하여 프레젠테이션에 테이블 이미지를 삽입하는 방법을 안내했습니다. PowerPoint 내에서 테이블을 관리하는 방법에 대해 자세히 알아보려면 Python을 사용하여 PowerPoint에서 테이블을 만드는 방법 항목을 참조하십시오.