Dl1Ft3Ht9 发表于 2024-8-29 22:39

Python宣讲家网批量下载PPT及合并PPT

```
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(, key=lambda x: int(x.split('.')))
      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()
```

LuckyClover 发表于 2024-8-30 08:20

感谢分享好东西,试一下

slzx10 发表于 2024-8-30 08:35

谢谢分享,能否生成exe文件?

静静想我1970 发表于 2024-8-30 09:53

感谢分享,研究一下

小浣浣 发表于 2024-8-30 10:34

大佬,怎么使用啊
页: [1]
查看完整版本: Python宣讲家网批量下载PPT及合并PPT