147 lines
4.4 KiB
Python
147 lines
4.4 KiB
Python
|
|
#!/usr/bin/env python
|
|||
|
|
#coding=utf-8
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import json
|
|||
|
|
import requests
|
|||
|
|
import datetime
|
|||
|
|
from urllib.parse import urlparse
|
|||
|
|
|
|||
|
|
|
|||
|
|
def read_a_txt():
|
|||
|
|
"""
|
|||
|
|
读取并解析 a.txt 文件,提取 PptExtraction 链接
|
|||
|
|
"""
|
|||
|
|
try:
|
|||
|
|
file_path = r'd:\Xuanchi\高斯泼溅\XCNote\tools\tongyi\a.txt'
|
|||
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|||
|
|
# 读取文件内容,跳过开头的 "响应数据: " 字符串
|
|||
|
|
content = f.read().replace('响应数据: ', '')
|
|||
|
|
data = json.loads(content)
|
|||
|
|
|
|||
|
|
ppt_extraction_url = data.get('Data', {}).get('Result', {}).get('PptExtraction')
|
|||
|
|
if ppt_extraction_url:
|
|||
|
|
print(f"成功提取 PptExtraction 链接: {ppt_extraction_url}")
|
|||
|
|
return ppt_extraction_url
|
|||
|
|
else:
|
|||
|
|
print("无法找到 PptExtraction 链接")
|
|||
|
|
return None
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"读取 a.txt 文件失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
|
|||
|
|
def download_ppt_extraction(url):
|
|||
|
|
"""
|
|||
|
|
下载并解析 PptExtraction 内容
|
|||
|
|
"""
|
|||
|
|
try:
|
|||
|
|
print(f"开始下载 PptExtraction 内容: {url}")
|
|||
|
|
response = requests.get(url, timeout=30)
|
|||
|
|
response.raise_for_status()
|
|||
|
|
|
|||
|
|
data = response.json()
|
|||
|
|
ppt_extraction_data = data.get('PptExtraction', {})
|
|||
|
|
key_frame_list = ppt_extraction_data.get('KeyFrameList', [])
|
|||
|
|
|
|||
|
|
print(f"成功下载并解析 PptExtraction 内容,共 {len(key_frame_list)} 张图片")
|
|||
|
|
return key_frame_list
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"下载 PptExtraction 内容失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return []
|
|||
|
|
|
|||
|
|
|
|||
|
|
def download_image(url, save_dir, timestamp, index):
|
|||
|
|
"""
|
|||
|
|
下载图片并保存到指定目录
|
|||
|
|
"""
|
|||
|
|
try:
|
|||
|
|
print(f"开始下载图片: {url}")
|
|||
|
|
response = requests.get(url, timeout=30)
|
|||
|
|
response.raise_for_status()
|
|||
|
|
|
|||
|
|
# 生成图片文件名:年月日_时分秒_序号.png
|
|||
|
|
image_filename = f"{timestamp}_{str(index).zfill(3)}.png"
|
|||
|
|
image_path = os.path.join(save_dir, image_filename)
|
|||
|
|
|
|||
|
|
# 保存图片
|
|||
|
|
with open(image_path, 'wb') as f:
|
|||
|
|
f.write(response.content)
|
|||
|
|
|
|||
|
|
print(f"成功下载图片: {image_filename}")
|
|||
|
|
return image_filename
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"下载图片失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""
|
|||
|
|
主函数
|
|||
|
|
"""
|
|||
|
|
print("===== 开始处理 PPT 提取结果 =====")
|
|||
|
|
|
|||
|
|
# 1. 读取并解析 a.txt 文件
|
|||
|
|
ppt_extraction_url = read_a_txt()
|
|||
|
|
if not ppt_extraction_url:
|
|||
|
|
print("无法提取 PptExtraction 链接,程序退出")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 2. 下载并解析 PptExtraction 内容
|
|||
|
|
key_frame_list = download_ppt_extraction(ppt_extraction_url)
|
|||
|
|
if not key_frame_list:
|
|||
|
|
print("无法获取 PPT 内容,程序退出")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 3. 生成时间戳(年月日_时分秒)
|
|||
|
|
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
|||
|
|
print(f"生成时间戳: {timestamp}")
|
|||
|
|
|
|||
|
|
# 4. 创建保存目录
|
|||
|
|
save_dir = r'd:\Xuanchi\高斯泼溅\XCNote\tools\tongyi\ppt_output'
|
|||
|
|
if not os.path.exists(save_dir):
|
|||
|
|
os.makedirs(save_dir)
|
|||
|
|
print(f"创建保存目录: {save_dir}")
|
|||
|
|
|
|||
|
|
# 5. 下载图片并整理结果
|
|||
|
|
result_dict = {}
|
|||
|
|
downloaded_count = 0
|
|||
|
|
|
|||
|
|
for i, frame in enumerate(key_frame_list):
|
|||
|
|
image_url = frame.get('FileUrl')
|
|||
|
|
if image_url:
|
|||
|
|
# 下载图片
|
|||
|
|
image_filename = download_image(image_url, save_dir, timestamp, i+1)
|
|||
|
|
if image_filename:
|
|||
|
|
downloaded_count += 1
|
|||
|
|
# 整理结果到字典
|
|||
|
|
result_dict[i+1] = {
|
|||
|
|
"Start": frame.get('Start'),
|
|||
|
|
"End": frame.get('End'),
|
|||
|
|
"ImageMd": f""
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if not downloaded_count:
|
|||
|
|
print("无法下载任何图片,程序退出")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 6. 输出整理结果
|
|||
|
|
print(f"\n===== 处理完成 =====")
|
|||
|
|
print(f"保存目录: {save_dir}")
|
|||
|
|
print(f"下载的图片数量: {downloaded_count}")
|
|||
|
|
print(f"\n整理结果:")
|
|||
|
|
print(json.dumps(result_dict, indent=4, ensure_ascii=False))
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|