import uno
import os

def export_slide_to_jpg_and_notes(input_file, output_dir, slide_number):
    # Start LibreOffice process
    localContext = uno.getComponentContext()
    resolver = localContext.ServiceManager.createInstanceWithContext("cohttp://m.sun.star.bridge.UnoUrlResolver", localContext)
    context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
    desktop = context.ServiceManager.createInstanceWithContext("cohttp://m.sun.star.frame.Desktop", context)
    
    # Load the presentation file
    file_url = uno.systemPathToFileUrl(os.path.abspath(input_file))
    document = desktop.loadComponentFromURL(file_url, "_blank", 0, ())

    if document:
        try:
            # Get slide count
            slide_count = document.getDrawPages().getCount()
            
            if slide_number > slide_count or slide_number < 1:
                print("Invalid slide number")
            else:
                # Get the slide
                slide = document.getDrawPages().getByIndex(slide_number - 1)
                
                # Export the slide as JPG
                output_file = os.path.join(output_dir, f"slide_{slide_number}.jpg")
                export_slide_as_jpg(slide, output_file)
                
                # Extract slide notes
                notes_text = extract_slide_notes(slide)
                notes_output_file = os.path.join(output_dir, f"slide_{slide_number}_notes.txt")
                save_notes_to_file(notes_text, notes_output_file)
                print(f"Slide {slide_number} exported as {output_file}")
                print(f"Slide {slide_number} notes exported as {notes_output_file}")
        finally:
            document.close(True)

def export_slide_as_jpg(slide, output_file):
    export_props = (
        ("FilterName", "impress_jpg_Export"),
        ("URL", uno.systemPathToFileUrl(output_file)),
        ("Quality", 90),
    )
    slide.exportToURL(*export_props)

def extract_slide_notes(slide):
    notes = slide.getNotes()
    if notes:
        return notes.PresentationNotes
    return ""

def save_notes_to_file(notes_text, output_file):
    with open(output_file, "w", encoding="utf-8") as file:
        file.write(notes_text)

# Usage
input_presentation = "path_to_your_presentation.odp"  # Replace with your presentation file path
output_directory = "output_folder"  # Replace with your desired output directory
slide_to_export = 1  # Replace with the slide number you want to export

export_slide_to_jpg_and_notes(input_presentation, output_directory, slide_to_export)

'컴퓨터 이야기~ > 소프트웨어' 카테고리의 다른 글

Retro Pie / Rasberrypie 에 retro 게임  (0) 2023.12.28
AWS Polly  (0) 2023.12.09
ffmpeg 로 더빙넣기.  (0) 2023.12.08
AWS Polly API  (0) 2023.12.08
Sendmail error log 가 쌓이는 문제  (0) 2023.12.03
,