63 lines
3.2 KiB
Python
63 lines
3.2 KiB
Python
import os
|
|
import tempfile
|
|
import xml.etree.ElementTree as ET
|
|
import zipfile
|
|
from typing import Dict
|
|
|
|
from desktop_env.evaluators.getters.file import get_vm_file
|
|
|
|
|
|
def get_audio_in_slide(env, config: Dict[str, str]):
|
|
ppt_file_path, slide_index, dest = config["ppt_file_path"], int(config["slide_index"]), config["dest"]
|
|
|
|
# Open the .pptx file as a zip file, fixme: now we assume there is only one audio file in the slides
|
|
audio_file_path = None
|
|
|
|
ppt_file_localhost_path = get_vm_file(env, {"path": ppt_file_path, "dest": os.path.split(ppt_file_path)[-1]})
|
|
|
|
with zipfile.ZipFile(ppt_file_localhost_path, 'r') as myzip:
|
|
# Find the relationships XML file for the first slide
|
|
slide1_rels_file = 'ppt/slides/_rels/slide{}.xml.rels'.format(slide_index + 1)
|
|
if slide1_rels_file in myzip.namelist():
|
|
with myzip.open(slide1_rels_file) as f:
|
|
# Parse the XML tree from the relationships file
|
|
tree = ET.parse(f)
|
|
root = tree.getroot()
|
|
# Define the namespace used in the relationships file
|
|
namespaces = {'r': 'http://schemas.openxmlformats.org/package/2006/relationships'}
|
|
# Look for all relationship elements that have a type attribute for audio
|
|
for rel in root.findall('r:Relationship', namespaces):
|
|
# Check if the relationship is for an audio file
|
|
if 'audio' in rel.attrib['Type']:
|
|
# The audio can be embedded inside the file or linked to an external file
|
|
# Get the target attribute which contains the audio file path
|
|
target = rel.attrib['Target']
|
|
|
|
if target.startswith('..'):
|
|
# Resolve the relative path to get the correct path within the zip file
|
|
audio_file_path = os.path.normpath(os.path.join('ppt/slides', target))
|
|
# Replace backslashes with forward slashes for ZIP compatibility
|
|
audio_file_path = audio_file_path.replace('\\', '/')
|
|
|
|
# Create a temporary directory to extract the audio file
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
# Extract the audio file
|
|
myzip.extract(audio_file_path, tmpdirname)
|
|
# Get the full path of the extracted audio file
|
|
extracted_audio_path = os.path.join(tmpdirname, audio_file_path)
|
|
# Return the extracted audio file path
|
|
audio_file_path = extracted_audio_path
|
|
|
|
else:
|
|
# the audio file is external to the .pptx file
|
|
# Return the audio file path
|
|
assert target.startswith("file://"), target
|
|
audio_file_path = target[7:]
|
|
|
|
if audio_file_path is None:
|
|
return None
|
|
|
|
else:
|
|
# Get the audio file from vm and return the file path in the host
|
|
return get_vm_file(env, {"path": audio_file_path, "dest": dest})
|