首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python操作wps在线表链接

python操作wps在线表链接

作者头像
软件小生活
发布2026-03-22 11:27:28
发布2026-03-22 11:27:28
1810
举报
文章被收录于专栏:软件小生活软件小生活

很多职业打工人,文职都会每天离不开操作wps在线表进行统计,或者每天都要统计数据进度通报给令人恶心的老板看。这件必做而又繁琐的事情。

下面我就想办法实现了用python写一个微信群自动通报进度的工具,思路是,首先用python实现一个自动化操作把在线表导出文件或者下载下来,然后用python操作本地Excel表文件(python目前没有办法直接操作wps在线链接表内容,也没有对应的aps云服务api,本地操作Excel表有很多方便操作的库),最后将操作表文件获得的内容数据通报到某个群。

这篇文章主要写python操作wps在线表,将其表内容下载到本地。后续篇幅写微信机器人定时群发消息,部署到任务计划定时执行。文件架构如下

首先要有操作浏览器的驱动,可以下载对应版本浏览器的驱动。你可以先在浏览器设置里面关于可以看到对应浏览器的版本,然后访问下面链接可以进行下载对应期次版本的驱动。

代码语言:javascript
复制
# 下载链接
Microsoft Edge WebDriver | Microsoft Edge Developer1

下面直接给出python直接操作wps无GUI桌面操作(无头模式),主要区别是多了无头模式。⚠️注意:部署到任务计划中,代码中的路径必须使用绝对路径,不然会找不到路径。

代码语言:javascript
复制
 # 3. ✅ 关键:启用无头模式(无图形界面)
options.add_argument("--headless=new")  # Edge 112+ 推荐使用 --headless=new
options.add_argument("--disable-gpu")   # 禁用 GPU 加速
options.add_argument("--no-sandbox")    # 禁用沙箱(任务计划必需)
options.add_argument("--disable-dev-shm-usage")  # 避免共享内存问题
options.add_argument("--remote-debugging-port=9222")  # 可选,便于调试

还有就是要使用独立用户数据目录,用完一定要在代码里进行删除,不然后台进程不会被删除,影响下次使用。

代码语言:javascript
复制
# ✅ 新增:使用独立用户数据目录(每次运行都新建)
self.user_data_dir = os.path.join(os.environ['TEMP'], f"edge_profile_{int(time.time())}")
os.makedirs(self.user_data_dir, exist_ok=True)
options.add_argument(f"--user-data-dir={self.user_data_dir}")
print(f"📁 使用独立用户数据目录: {self.user_data_dir}")

杀死进程清理资源

代码语言:javascript
复制
def cleanup(self):
    """优化清理资源 - 确保浏览器完全关闭"""
    if self.driver:
        print("\n⏳ 正在彻底关闭浏览器...")

        try:
            # 1. 先正常退出
            self.driver.quit()
            print("✅ 已发送quit命令")
        except Exception as e:
            print(f"⚠️ 正常退出失败: {e}")

        # 2. 强制杀死Edge相关进程
        self._kill_edge_processes()

        # 3. 清理临时文件
        self._clear_temp_files()

        # 4. 等待进程完全结束
        time.sleep(2)
        print("✅ 浏览器清理完成")

def _kill_edge_processes(self):
    """强制杀死Edge相关进程"""
    import subprocess
    import signal
    import psutil  # 需要安装: pip install psutil

    processes_to_kill = [
        "msedge.exe",           # Edge主进程
        "msedgewebview2.exe",   # Edge WebView2
        "msedgedriver.exe",     # EdgeDriver
        "msedgeupdate.exe",     # Edge更新进程
    ]

    for proc_name in processes_to_kill:
        try:
            # 方法1: 使用taskkill
            subprocess.run(f"taskkill /f /im {proc_name}", 
                          shell=True, capture_output=True)

            # 方法2: 使用psutil确保彻底清理
            for proc in psutil.process_iter(['pid', 'name']):
                try:
                    if proc.info['name'] and proc.info['name'].lower() == proc_name.lower():
                        proc.terminate()  # 优雅终止
                        time.sleep(0.5)
                        if proc.is_running():
                            proc.kill()  # 强制终止
                        print(f"✅ 已终止进程: {proc_name} (PID: {proc.info['pid']})")
                except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
                    continue
        except Exception as e:
            print(f"⚠️ 终止{proc_name}时出错: {e}")

def _clear_temp_files(self):
    """清理临时文件和锁文件"""
    import tempfile

    # 清理Edge临时目录
    edge_temp_paths = [
        os.path.join(os.environ.get('TEMP', ''), "MicrosoftEdge*.tmp"),
        os.path.join(os.environ.get('TEMP', ''), "edge_headless_profile"),
        os.path.join(os.environ.get('LOCALAPPDATA', ''), "Temp", "MicrosoftEdge*.tmp"),
    ]

    for pattern in edge_temp_paths:
        try:
            import glob
            for file in glob.glob(pattern):
                try:
                    if os.path.isdir(file):
                        import shutil
                        shutil.rmtree(file, ignore_errors=True)
                    else:
                        os.remove(file)
                except:
                    pass
        except:
            pass

    print("✅ 临时文件已清理")

1、使用cookie登录操作wps在线表进行导出(下载)文件完整代码如下:

代码语言:javascript
复制
# 使用cookie登录导出文件.py
import json
import time
import os
import shutil
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import traceback


class WPSAutoExport:
    """使用Cookie自动登录并执行导出流程"""

    def __init__(self):
        # 您提供的正确文档链接
        self.doc_url = "https://www.kdocs.cn/l/*****" #替换为你的wps在线链接
        self.cookie_file = "wps_cookies.json"

        # 导出相关配置
        self.default_download_dir = os.path.join(os.path.expanduser("~"), "Downloads")
        self.target_export_dir = r"E:\Wechat_XiaoMei_Notice\wps截图通报"
        self.export_filename = "天馈调整表.xlsx"
        self.export_file_path = None
        self.driver = None

        # 窗口配置
        self.window_mode = "maximized"  # 可选: "maximized", "fullscreen", "custom"
        self.custom_size = (1400, 900)  # 当window_mode为"custom"时使用

        # 等待配置
        self.wait_timeout = 15
        self.element_wait_timeout = 10

        # 确保目录存在
        os.makedirs(self.default_download_dir, exist_ok=True)
        os.makedirs(self.target_export_dir, exist_ok=True)

    def setup_browser(self):
        """配置浏览器选项"""
        print("🔧 正在配置浏览器...")

        try:
            # 设置Edge选项
            options = Options()

            # 禁用自动化特征检测
            options.add_argument("--disable-blink-features=AutomationControlled")
            options.add_experimental_option("excludeSwitches", ["enable-automation"])
            options.add_experimental_option('useAutomationExtension', False)

            # 设置下载目录
            prefs = {
                "download.default_directory": self.default_download_dir,
                "download.prompt_for_download": False,
                "download.directory_upgrade": True,
                "safebrowsing.enabled": True
            }
            options.add_experimental_option("prefs", prefs)

            # 性能优化
            options.add_argument("--disable-gpu")
            options.add_argument("--disable-software-rasterizer")

            # 3. ✅ 关键:启用无头模式(无图形界面)
            options.add_argument("--headless=new")  # Edge 112+ 推荐使用 --headless=new
            options.add_argument("--disable-gpu")   # 禁用 GPU 加速
            options.add_argument("--no-sandbox")    # 禁用沙箱(任务计划必需)
            options.add_argument("--disable-dev-shm-usage")  # 避免共享内存问题
            options.add_argument("--remote-debugging-port=9222")  # 可选,便于调试

            # ✅ 新增:使用独立用户数据目录(每次运行都新建)
            self.user_data_dir = os.path.join(os.environ['TEMP'], f"edge_profile_{int(time.time())}")
            os.makedirs(self.user_data_dir, exist_ok=True)
            options.add_argument(f"--user-data-dir={self.user_data_dir}")
            print(f"📁 使用独立用户数据目录: {self.user_data_dir}")

            # 指定Edge驱动路径 - 绝对路径
            edge_driver_path = r"E:\Wechat_XiaoMei_Notice\通报天线调整进度小区数\msedgedriver.exe"

            # 创建服务实例
            service = Service(executable_path=edge_driver_path)

            # 启动浏览器
            self.driver = webdriver.Edge(service=service, options=options)

            # 设置窗口大小
            self.set_window_size()

            print("✅ 浏览器启动成功")
            return True

        except Exception as e:
            print(f"❌ 浏览器启动失败: {e}")
            traceback.print_exc()
            return False

    def set_window_size(self):
        """设置浏览器窗口大小"""
        try:
            if self.window_mode == "maximized":
                # 最大化窗口
                self.driver.maximize_window()
                print("✅ 窗口已最大化")

                # 获取实际窗口尺寸
                time.sleep(0.5)
                window_size = self.driver.get_window_size()
                print(f"🪟 窗口尺寸: {window_size['width']}x{window_size['height']}")

            elif self.window_mode == "fullscreen":
                # 全屏模式
                self.driver.fullscreen_window()
                print("✅ 窗口已全屏")

            elif self.window_mode == "custom":
                # 自定义大小
                width, height = self.custom_size
                self.driver.set_window_size(width, height)
                print(f"🪟 窗口尺寸设置为: {width}x{height}")

            else:
                # 默认最大化
                self.driver.maximize_window()
                print("✅ 窗口已最大化")

        except Exception as e:
            print(f"⚠️ 设置窗口大小失败: {e}")
            # 尝试备用方案
            try:
                self.driver.maximize_window()
                print("✅ 备用方案: 窗口已最大化")
            except:
                pass

    def load_cookies(self):
        """从文件加载Cookies"""
        print(f"📂 正在加载Cookies: {self.cookie_file}")

        if not os.path.exists(self.cookie_file):
            print(f"❌ 未找到Cookie文件: {self.cookie_file}")
            print("请先运行 wps_login_get_cookie.py 获取Cookies")
            return False

        try:
            with open(self.cookie_file, 'r', encoding='utf-8') as f:
                cookies = json.load(f)

            # 先访问主域名以设置Cookies
            self.driver.get("https://www.kdocs.cn")
            time.sleep(2)

            # 清除现有Cookies
            self.driver.delete_all_cookies()

            # 添加Cookies
            cookie_count = 0
            for cookie in cookies:
                try:
                    # 移除可能的问题字段
                    if 'sameSite' in cookie and cookie['sameSite'] not in ['Strict', 'Lax', 'None']:
                        del cookie['sameSite']
                    self.driver.add_cookie(cookie)
                    cookie_count += 1
                except Exception as e:
                    print(f"⚠️ 跳过无效Cookie: {e}")
                    continue

            print(f"✅ 成功加载 {cookie_count} 个Cookies")
            return True

        except Exception as e:
            print(f"❌ 加载Cookies失败: {e}")
            traceback.print_exc()
            return False

    def access_document(self):
        """访问文档并检查登录状态"""
        print(f"🌐 正在访问文档: {self.doc_url}")

        # 使用Cookies访问文档
        self.driver.get(self.doc_url)
        time.sleep(5)

        # 检查是否登录成功
        current_url = self.driver.current_url
        if "kdocs.cn/l/ctb28nppB2cv" in current_url:
            print("✅ 登录成功,已进入目标文档页面")

            # 等待页面加载完成
            try:
                WebDriverWait(self.driver, 10).until(
                    EC.presence_of_element_located((By.XPATH,
                                                    "//div[contains(@class, 'kdocs-content')] | //div[contains(@class, 'sheet-container')]"))
                )
                print("✅ 文档内容已加载")
            except:
                print("⚠️ 文档内容加载较慢,继续执行...")

            return True
        else:
            print("⚠️ 登录可能失败,当前页面:", current_url)

            # 检查是否有登录提示
            try:
                login_elements = self.driver.find_elements(By.XPATH,
                                                           "//button[contains(text(), '登录')] | //div[contains(text(), '登录')]")
                if login_elements:
                    print("❌ 检测到登录页面,Cookie可能已失效")
                    return False
            except:
                pass

            return True  # 假设成功

    def smart_find_and_click(self, selectors, description, max_wait=10, retry_count=3):
        """
        智能查找并点击元素
        selectors: [(selector, type), ...]  type可以是 'xpath' 或 'css'
        description: 元素描述
        """
        for attempt in range(retry_count):
            print(f"🔍 第{attempt + 1}次尝试查找{description}...")

            for selector, selector_type in selectors:
                try:
                    if selector_type == 'xpath':
                        element = WebDriverWait(self.driver, max_wait).until(
                            EC.element_to_be_clickable((By.XPATH, selector))
                        )
                    else:  # css
                        element = WebDriverWait(self.driver, max_wait).until(
                            EC.element_to_be_clickable((By.CSS_SELECTOR, selector))
                        )

                    if element.is_displayed():
                        # 滚动到元素位置
                        self.driver.execute_script(
                            "arguments[0].scrollIntoView({block: 'center', behavior: 'smooth'});", element)
                        time.sleep(0.5)

                        # 点击元素
                        element.click()
                        print(f"✅ 成功点击{description}: {selector[:50]}...")
                        time.sleep(1.5)  # 等待操作完成
                        return True

                except Exception as e:
                    continue

            if attempt < retry_count - 1:
                print(f"⚠️ 第{attempt + 1}次尝试失败,等待2秒后重试...")
                time.sleep(2)

        print(f"❌ 查找{description}失败,尝试了{retry_count}次")
        return False

    def click_more_menu_smart(self):
        """智能点击更多菜单按钮"""
        print("=" * 40)
        print("1️⃣ 开始点击更多菜单")
        print("=" * 40)

        more_menu_selectors = [
            ("//button[contains(@class, 'kd-button-light') and .//i[contains(@class, 'kd-icon-menu')]]", 'xpath'),
            ("//button[contains(@class, 'kd-button') and .//i[contains(@class, 'kd-icon-menu')]]", 'xpath'),
            ("//button[.//i[contains(@class, 'kd-icon-menu')]]", 'xpath'),
            ("//button[contains(@class, 'kd-icon-menu')]", 'xpath'),
            ("//button[contains(@title, '更多') or contains(@aria-label, '更多')]", 'xpath'),
            ("//span[contains(text(), '更多')]/..", 'xpath'),
            ("button.kd-button.kd-button-light.kd-button-lg.kd-button-icon", 'css')
        ]

        return self.smart_find_and_click(more_menu_selectors, "更多菜单按钮")

    def click_download_option_smart(self):
        """智能点击下载选项"""
        print("=" * 40)
        print("2️⃣ 开始点击下载选项")
        print("=" * 40)

        download_selectors = [
            ("//div[contains(@class, 'menu-text') and .//span[text()='下载']]", 'xpath'),
            ("//span[text()='下载']", 'xpath'),
            ("//div[text()='下载']", 'xpath'),
            ("//button[text()='下载']", 'xpath'),
            ("//i[contains(@class, 'kd-icon-transmit_download')]/..", 'xpath'),
            ("//div[@data-key='Download']", 'xpath'),
            ("//div[@data-key='DownLoad']", 'xpath')
        ]

        return self.smart_find_and_click(download_selectors, "下载选项")

    def handle_export_dialog_smart(self):
        """
        智能处理导出对话框
        两种情况:
        1. 显示"导出为 Excel"按钮
        2. 显示"普通下载"按钮
        """
        print("=" * 40)
        print("3️⃣ 处理导出对话框(智能识别两种按钮)")
        print("=" * 40)

        # 首先等待对话框出现
        print("⏳ 等待导出对话框出现...")

        # 检查对话框是否已经出现
        dialog_selectors = [
            "//div[contains(@class, 'export-dialog')]",
            "//div[contains(@class, 'download-dialog')]",
            "//div[contains(text(), '下载方式')]",
            "//div[contains(text(), '导出方式')]"
        ]

        dialog_found = False
        for selector in dialog_selectors:
            try:
                element = WebDriverWait(self.driver, 5).until(
                    EC.presence_of_element_located((By.XPATH, selector))
                )
                if element.is_displayed():
                    dialog_found = True
                    print("✅ 检测到导出对话框")
                    break
            except:
                continue

        if not dialog_found:
            print("⚠️ 未检测到导出对话框,但继续尝试...")

        # 情况1:优先尝试"导出为 Excel"按钮
        print("🔍 首先尝试查找'导出为 Excel'按钮...")
        export_excel_selectors = [
            ("//button[contains(text(), '导出为 Excel')]", 'xpath'),
            ("//button[contains(@class, 'export-btn') and contains(text(), '导出')]", 'xpath'),
            ("//button[.//span[contains(text(), '导出为 Excel')]]", 'xpath'),
            ("//div[contains(text(), '导出为 Excel')]", 'xpath'),
            ("/html/body/div[5]/div[4]/div/div[2]/div/div/div[2]/button", 'xpath')
        ]

        for selector, selector_type in export_excel_selectors:
            try:
                if selector_type == 'xpath':
                    element = WebDriverWait(self.driver, 5).until(
                        EC.element_to_be_clickable((By.XPATH, selector))
                    )
                else:
                    element = WebDriverWait(self.driver, 5).until(
                        EC.element_to_be_clickable((By.CSS_SELECTOR, selector))
                    )

                if element.is_displayed():
                    # 获取按钮文本以便确认
                    button_text = element.text.strip() or element.get_attribute('textContent').strip()
                    print(f"✅ 找到'导出为 Excel'按钮: {button_text}")

                    # 点击按钮
                    element.click()
                    print("✅ 点击'导出为 Excel'按钮成功")
                    time.sleep(3)  # 等待导出处理
                    return True

            except Exception as e:
                continue

        # 情况2:如果没找到"导出为 Excel",尝试"普通下载"按钮
        print("🔍 未找到'导出为 Excel'按钮,尝试查找'普通下载'按钮...")
        normal_download_selectors = [
            ("//button[contains(text(), '普通下载')]", 'xpath'),
            ("//button[contains(@class, 'kdv-button--secondary') and contains(text(), '普通下载')]", 'xpath'),
            ("//button[.//span[contains(text(), '普通下载')]]", 'xpath'),
            ("//button[text()='普通下载']", 'xpath')
        ]

        for selector, selector_type in normal_download_selectors:
            try:
                if selector_type == 'xpath':
                    element = WebDriverWait(self.driver, 5).until(
                        EC.element_to_be_clickable((By.XPATH, selector))
                    )
                else:
                    element = WebDriverWait(self.driver, 5).until(
                        EC.element_to_be_clickable((By.CSS_SELECTOR, selector))
                    )

                if element.is_displayed():
                    # 获取按钮文本以便确认
                    button_text = element.text.strip() or element.get_attribute('textContent').strip()
                    print(f"✅ 找到'普通下载'按钮: {button_text}")

                    # 点击按钮
                    element.click()
                    print("✅ 点击'普通下载'按钮成功")
                    time.sleep(3)  # 等待下载处理
                    return True

            except Exception as e:
                continue

        print("❌ 既没找到'导出为 Excel'也没找到'普通下载'按钮")
        return False

    def wait_for_download_complete(self, timeout=60, check_interval=2):
        """等待下载完成"""
        print("⏳ 等待下载完成...")

        start_time = time.time()
        initial_files = set()

        # 获取初始文件列表
        if os.path.exists(self.default_download_dir):
            initial_files = set(os.listdir(self.default_download_dir))

        while time.time() - start_time < timeout:
            # 检查下载目录是否有新文件
            if os.path.exists(self.default_download_dir):
                current_files = set(os.listdir(self.default_download_dir))
                new_files = current_files - initial_files

                # 检查是否有新的.xlsx文件
                new_xlsx_files = [f for f in new_files if f.endswith('.xlsx')]

                if new_xlsx_files:
                    print(f"✅ 检测到新下载的.xlsx文件: {', '.join(new_xlsx_files)}")

                    # 检查文件是否完全下载完成(文件大小稳定)
                    file_complete = True
                    for filename in new_xlsx_files:
                        file_path = os.path.join(self.default_download_dir, filename)
                        try:
                            size1 = os.path.getsize(file_path)
                            time.sleep(0.5)
                            size2 = os.path.getsize(file_path)

                            if size1 != size2 or size1 == 0:
                                file_complete = False
                                print(f"⚠️ 文件 {filename} 仍在下载中...")
                                break
                        except:
                            file_complete = False

                    if file_complete:
                        print("✅ 所有文件下载完成")
                        return True

            # 检查页面是否有下载完成提示
            try:
                success_selectors = [
                    "//div[contains(text(), '下载成功')]",
                    "//div[contains(text(), '导出成功')]",
                    "//div[contains(text(), '保存成功')]"
                ]

                for selector in success_selectors:
                    try:
                        element = self.driver.find_element(By.XPATH, selector)
                        if element.is_displayed():
                            print(f"✅ 检测到成功提示: {element.text}")
                            return True
                    except:
                        continue
            except:
                pass

            time.sleep(check_interval)

        print(f"⚠️ 等待下载超时({timeout}秒)")
        return False

    def handle_exported_file(self):
        """
        处理导出的Excel文件
        1. 查找下载的文件
        2. 复制到目标目录
        3. 清理下载目录
        """
        print("\n" + "=" * 50)
        print("🚀 开始处理导出的Excel文件")
        print("=" * 50)

        try:
            # 1. 等待文件下载完成
            print("⏳ 等待文件下载完成...")
            time.sleep(3)

            # 2. 查找下载的文件
            print(f"🔍 在下载目录查找文件: {self.default_download_dir}")

            # 搜索所有包含"天馈调整表"的文件
            keyword = "天馈调整表"
            found_files = []

            if os.path.exists(self.default_download_dir):
                for filename in os.listdir(self.default_download_dir):
                    if keyword in filename and filename.endswith('.xlsx'):
                        file_path = os.path.join(self.default_download_dir, filename)
                        if os.path.isfile(file_path):
                            found_files.append(file_path)

            if not found_files:
                print(f"❌ 在下载目录中未找到包含'{keyword}'的xlsx文件")

                # 尝试查找任何.xlsx文件
                all_xlsx_files = []
                for filename in os.listdir(self.default_download_dir):
                    if filename.endswith('.xlsx'):
                        file_path = os.path.join(self.default_download_dir, filename)
                        if os.path.isfile(file_path):
                            all_xlsx_files.append(file_path)

                if all_xlsx_files:
                    found_files = all_xlsx_files
                    print(f"⚠️ 但找到了 {len(found_files)} 个.xlsx文件,将使用这些文件")
                else:
                    print(f"📁 下载目录内容:")
                    try:
                        files = os.listdir(self.default_download_dir)
                        for f in files[:10]:
                            print(f"  - {f}")
                    except:
                        pass
                    return False

            # 3. 找到最新下载的文件
            print(f"✅ 找到 {len(found_files)} 个相关文件:")
            for f in found_files:
                print(f"📄 {os.path.basename(f)}")

            # 按修改时间排序,取最新的文件
            latest_file = max(found_files, key=os.path.getmtime)
            print(f"📅 最新文件: {os.path.basename(latest_file)}")
            print(f"📁 文件路径: {latest_file}")
            print(f"📏 文件大小: {os.path.getsize(latest_file) / 1024:.2f} KB")

            # 保存文件路径
            self.export_file_path = latest_file

            # 4. 复制文件到目标目录
            print(f"\n🔧 复制文件到目标目录: {self.target_export_dir}")

            # 确保目标目录存在
            os.makedirs(self.target_export_dir, exist_ok=True)

            # 目标文件路径
            target_file = os.path.join(self.target_export_dir, self.export_filename)

            # 如果目标文件已存在,先删除
            if os.path.exists(target_file):
                print(f"⚠️ 目标文件已存在,正在删除: {os.path.basename(target_file)}")
                os.remove(target_file)

            # 复制文件
            shutil.copy2(latest_file, target_file)

            if os.path.exists(target_file):
                print(f"✅ 文件复制成功!")
                print(f"📁 源文件: {os.path.basename(latest_file)}")
                print(f"📁 目标文件: {os.path.basename(target_file)}")
                print(f"📁 目标路径: {target_file}")
                print(f"📏 目标文件大小: {os.path.getsize(target_file) / 1024:.2f} KB")
            else:
                print(f"❌ 文件复制失败")
                return False

            # 5. 清理下载目录
            print(f"\n🧹 清理下载目录...")
            deleted_count = 0

            for file_path in found_files:
                try:
                    os.remove(file_path)
                    print(f"  🗑️ 已删除: {os.path.basename(file_path)}")
                    deleted_count += 1
                except Exception as e:
                    print(f"  ⚠️ 删除失败 {os.path.basename(file_path)}: {e}")

            print(f"✅ 清理完成,共删除 {deleted_count} 个文件")

            return True

        except Exception as e:
            print(f"❌ 处理导出文件时出错: {e}")
            traceback.print_exc()
            return False

    def cleanup(self):
        """清理资源"""
        if self.driver:
            print("\n⏳ 正在关闭浏览器...")
            time.sleep(3)
            self.driver.quit()
            print("✅ 浏览器已关闭")

    def run_single_attempt(self):
        """单次运行完整的导出流程"""
        try:
            # 1. 设置浏览器
            if not self.setup_browser():
                return False

            # 2. 加载Cookies
            if not self.load_cookies():
                return False

            # 3. 访问文档
            if not self.access_document():
                return False

            # 4. 点击更多菜单
            if not self.click_more_menu_smart():
                print("❌ 点击更多菜单失败")
                return False

            # 5. 点击下载选项
            if not self.click_download_option_smart():
                print("❌ 点击下载选项失败")
                return False

            # 6. 智能处理导出对话框
            if not self.handle_export_dialog_smart():
                print("❌ 处理导出对话框失败")
                return False

            # 7. 等待下载完成
            if not self.wait_for_download_complete():
                print("⚠️ 等待下载可能未完成,继续处理...")

            # 8. 处理导出的文件
            if not self.handle_exported_file():
                print("❌ 处理导出文件失败")
                return False

            print("\n" + "=" * 60)
            print("🎉 导出流程完成!")
            print(f"📁 文件已保存到: {os.path.join(self.target_export_dir, self.export_filename)}")
            print("=" * 60)
            return True

        except Exception as e:
            print(f"❌ 程序执行出错: {e}")
            traceback.print_exc()
            return False
        finally:
            self.cleanup()


if __name__ == "__main__":
    print("🚀 开始WPS自动导出流程")
    print("=" * 60)
    tool = WPSAutoExport()
    success = tool.run_single_attempt()

    if success:
        print("\n" + "=" * 60)
        print("✅ 自动导出流程成功完成!")
        print("=" * 60)
    else:
        print("\n" + "=" * 60)
        print("❌ 自动导出流程失败")
        print("=" * 60)

首次登录获取cookie信息,如果你第一次进行部署,一定是要先手动登录一次获取cookie信息。

代码语言:javascript
复制
# 登录wps在线表获取cookie.py
import json
import time
import os
import shutil
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class WPSLoginToGetCookie:
    """专门用于手动登录并获取Cookie的工具"""

    def __init__(self):
        self.doc_url = "https://www.kdocs.cn/l/******"
        self.cookie_file = "wps_cookies.json"
        self.driver = None

    def setup_browser_simple(self):
        """简化版浏览器配置"""
        print("🔧 正在配置浏览器(简化版)...")

        try:
            # 使用最基本的配置
            options = Options()

            # 移除可能导致问题的复杂配置
            # options.add_argument("--window-size=1400,900")
            # options.add_argument("--disable-blink-features=AutomationControlled")
            # options.add_experimental_option("excludeSwitches", ["enable-automation"])
            # options.add_experimental_option('useAutomationExtension', False)

            # 启动浏览器
            self.driver = webdriver.Edge(options=options)
            print("✅ 浏览器启动成功")

            # 最大化窗口
            self.driver.maximize_window()
            print("✅ 窗口已最大化")

            return True

        except Exception as e:
            print(f"❌ 浏览器启动失败: {e}")
            print("\n💡 可能的解决方案:")
            print("1. 检查是否已安装Microsoft Edge浏览器")
            print("2. 检查Edge浏览器版本")
            print("3. 检查是否安装了正确的EdgeDriver")
            print("4. 尝试以管理员身份运行Python脚本")
            return False

    def manual_login_and_save(self):
        """手动登录并保存Cookies"""
        print("=" * 60)
        print("🔐 请在打开的浏览器中手动登录WPS账号")
        print("步骤:")
        print("1. 在浏览器中完成登录(可能需要滑块验证)")
        print("2. 登录成功后,确保当前页面是目标文档页面")
        print("3. 确认无误后,按回车键保存Cookies")
        print("=" * 60)

        # 访问文档页面
        self.driver.get(self.doc_url)
        time.sleep(5)

        # 检查是否需要登录
        try:
            login_elements = self.driver.find_elements(By.XPATH,
                "//button[contains(text(), '登录')] | //div[contains(text(), '登录')]")
            if login_elements:
                print("⚠️ 检测到需要登录,请手动登录...")
        except:
            pass

        # 等待用户手动登录
        input("\n📌 登录完成后,请按回车键继续保存Cookies...")

        try:
            # 获取Cookies
            cookies = self.driver.get_cookies()

            # 保存到文件
            with open(self.cookie_file, 'w', encoding='utf-8') as f:
                json.dump(cookies, f, ensure_ascii=False, indent=2)

            print(f"\n✅ Cookies已成功保存到: {os.path.abspath(self.cookie_file)}")
            print(f"📄 共保存 {len(cookies)} 个Cookies")

            # 显示前几个重要的Cookie
            print("\n📋 保存的重要Cookie:")
            for cookie in cookies[:3]:
                if 'name' in cookie and 'value' in cookie:
                    name = cookie['name']
                    value_preview = cookie['value'][:20] + "..." if len(cookie['value']) > 20 else cookie['value']
                    print(f"  {name}: {value_preview}")

            return True

        except Exception as e:
            print(f"❌ 保存Cookies失败: {e}")
            return False

    def cleanup(self):
        """清理资源"""
        if self.driver:
            print("\n⏳ 正在关闭浏览器...")
            time.sleep(2)
            self.driver.quit()
            print("✅ 浏览器已关闭")

    def run(self):
        """运行主流程"""
        print("🚀 WPS手动登录获取Cookie工具")
        print("=" * 60)

        # 检查是否已有Cookie文件
        if os.path.exists(self.cookie_file):
            print(f"⚠️ 检测到已存在的Cookie文件: {self.cookie_file}")
            choice = input("是否覆盖?(y/n): ").strip().lower()
            if choice != 'y':
                print("❌ 已取消操作")
                return False

        try:
            if not self.setup_browser_simple():
                return False

            if self.manual_login_and_save():
                print("\n🎉 登录和Cookie保存完成!")
                print("现在你可以运行 auto_export.py 来进行自动导出操作了。")
                return True
            else:
                print("❌ 登录或保存Cookies失败")
                return False

        except Exception as e:
            print(f"❌ 程序执行出错: {e}")
            return False
        finally:
            self.cleanup()


if __name__ == "__main__":
    tool = WPSLoginToGetCookie()
    tool.run()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2026-03-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件小生活 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档