吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 712|回复: 4
收起左侧

[学习记录] Python宣讲家网批量下载PPT及合并PPT

[复制链接]
Dl1Ft3Ht9 发表于 2024-8-29 22:39
import requests
import os
import win32com.client
from bs4 import BeautifulSoup
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter.ttk import Progressbar

def get_ppt_count(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')
    scripts = soup.find_all('script')
    count = 0
    for script in scripts:
        if 'photos.push' in script.text:
            count += script.text.count('photos.push')
    return count

def download_ppts(base_url, count, download_folder, progress_var, progress_bar):
    if not os.path.exists(download_folder):
        os.makedirs(download_folder)

    progress_var.set(0)
    progress_bar['maximum'] = count

    for i in range(1, count + 1):
        url = base_url.format(i)
        response = requests.get(url)
        if response.status_code == 200:
            with open(os.path.join(download_folder, f"{i}.pptx"), 'wb') as file:
                file.write(response.content)
        else:
            print(f"下载失败: {url}")

        progress_var.set(i)
        progress_bar.update_idletasks()

def merge_ppt_files(folder_path, output_file, progress_var, progress_bar):
    try:
        wps = win32com.client.Dispatch("KWPP.Application")
        merged_presentation = wps.Presentations.Add()

        ppt_files = sorted([f for f in os.listdir(folder_path) if f.endswith(".pptx")], key=lambda x: int(x.split('.')[0]))
        progress_var.set(0)
        progress_bar['maximum'] = len(ppt_files)

        for filename in ppt_files:
            ppt_file = os.path.join(folder_path, filename)
            presentation = wps.Presentations.Open(ppt_file, ReadOnly=True)
            for slide in presentation.Slides:
                slide.Copy()
                merged_presentation.Slides.Paste()
            presentation.Close()

            progress_var.set(progress_var.get() + 1)
            progress_bar.update_idletasks()

        merged_presentation.SaveAs(output_file)
        merged_presentation.Close()
        wps.Quit()

    except Exception as e:
        messagebox.showerror("错误", f"WPS Office合并失败: {e}\n尝试使用 PowerPoint 合并")
        try:
            ppt_app = win32com.client.Dispatch("PowerPoint.Application")
            merged_presentation = ppt_app.Presentations.Add()
            for filename in ppt_files:
                ppt_file = os.path.join(folder_path, filename)
                presentation = ppt_app.Presentations.Open(ppt_file, ReadOnly=True)
                for slide in presentation.Slides:
                    slide.Copy()
                    merged_presentation.Slides.Paste()
                presentation.Close()

            merged_presentation.SaveAs(output_file)
            merged_presentation.Close()
            ppt_app.Quit()
        except Exception as e:
            messagebox.showerror("错误", f"PowerPoint合并失败: {e}")

def run_task():
    url = url_entry.get()
    download_folder = folder_path_var.get()
    output_file = output_file_var.get()

    if not url or not download_folder or not output_file:
        messagebox.showwarning("警告", "所有字段都是必需的!")
        return

    ppt_count = get_ppt_count(url)
    id = url.split('.')[-2].split('/')[-1]
    base_url = f"http://upload.71.cn/pptnotes/{id}/{id}_{{}}.pptx"

    # 更新进度条状态
    download_task(progress_var, progress_bar, base_url, ppt_count, download_folder)
    merge_task(progress_var, progress_bar, download_folder, output_file)

def download_task(progress_var, progress_bar, base_url, ppt_count, download_folder):
    progress_var.set(0)
    download_ppts(base_url, ppt_count, download_folder, progress_var, progress_bar)
    progress_var.set(0)
    progress_bar['value'] = 0

def merge_task(progress_var, progress_bar, download_folder, output_file):
    progress_var.set(0)
    merge_ppt_files(download_folder, output_file, progress_var, progress_bar)
    progress_var.set(0)
    progress_bar['value'] = 0
    messagebox.showinfo("完成", "操作完成!")

def browse_folder():
    folder_selected = filedialog.askdirectory()
    folder_path_var.set(folder_selected)

def browse_file():
    file_selected = filedialog.asksaveasfilename(defaultextension=".pptx", filetypes=[("PowerPoint Files", "*.pptx")])
    output_file_var.set(file_selected)

# 创建主窗口
root = tk.Tk()
root.title("宣讲家网PPT下载和合并工具")

# 创建并布局组件
tk.Label(root, text="PPT 页面 URL:").pack(padx=10, pady=5)
url_entry = tk.Entry(root, width=50)
url_entry.pack(padx=10, pady=5)

tk.Label(root, text="下载文件夹:").pack(padx=10, pady=5)
folder_path_var = tk.StringVar()
folder_entry = tk.Entry(root, textvariable=folder_path_var, width=50)
folder_entry.pack(padx=10, pady=5)
tk.Button(root, text="浏览", command=browse_folder).pack(padx=10, pady=5)

tk.Label(root, text="输出文件名:").pack(padx=10, pady=5)
output_file_var = tk.StringVar()
output_entry = tk.Entry(root, textvariable=output_file_var, width=50)
output_entry.pack(padx=10, pady=5)
tk.Button(root, text="浏览", command=browse_file).pack(padx=10, pady=5)

tk.Button(root, text="开始", command=run_task).pack(padx=10, pady=10)

# 进度条
progress_var = tk.DoubleVar()
progress_bar = Progressbar(root, variable=progress_var, length=400)
progress_bar.pack(padx=10, pady=10)

# 运行主事件循环
root.mainloop()

免费评分

参与人数 3吾爱币 +3 热心值 +3 收起 理由
静静想我1970 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
LuckyClover + 1 + 1 谢谢@Thanks!
vethenc + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

LuckyClover 发表于 2024-8-30 08:20
感谢分享好东西,试一下
slzx10 发表于 2024-8-30 08:35
静静想我1970 发表于 2024-8-30 09:53
小浣浣 发表于 2024-8-30 10:34
大佬,怎么使用啊
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-11 17:30

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表