You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS下Python导出并打开Excel文件问题求助(Windows迁移)

Fixing Your Python Excel Export & Launch Issue on macOS

Let's work through the macOS compatibility problems in your code step by step. The main issues are Windows-specific hardcoded paths, confusing file handling, and an incorrect way to launch Excel. Here's how to adjust everything:

1. Fix File Saving & Path Confusion

  • Windows vs macOS Paths: macOS doesn't use C:/ paths. By default, your random.xls is saved in the current working directory (the folder where you run your Python script). To make this explicit and easy to find, use the os module to get and print the path.
  • Redundant Temporary File: Your code calls book.save(name) then book.save(TemporaryFile()) — this second call is unnecessary and might be causing confusion. Remove it unless you specifically need an in-memory temporary file.

2. Correctly Launch Excel on macOS

Instead of hardcoding the Excel executable path (which is totally different on macOS), use the system's open command to launch the file. This works regardless of where Excel is installed on the system.

Modified Main Function Code

import os
import subprocess
from datetime import datetime
import selenium.webdriver as webdriver
import xlwt
from xlwt import Workbook

def main():
    now = datetime.now()
    option = webdriver.ChromeOptions()
    browser = webdriver.Chrome(options=option)  # Added options to Chrome initialization
    
    keyword = None
    urlList = None 
    wi = webInfo(browser, keyword)
    mi = mobileInfo(browser, keyword)
    urlList = wi + mi
    
    browser.close()
    
    # Create and populate Excel file
    book = Workbook()
    sheet1 = book.add_sheet('sheet1')
    for i in range(len(urlList)):
        for j in range(len(urlList[i])):
            sheet1.write(i, j, urlList[i][j])
    
    # Define file name and path
    file_name = "random.xls"
    current_dir = os.getcwd()  # Get current working directory
    excel_file_path = os.path.join(current_dir, file_name)
    
    # Save the file and confirm success
    book.save(excel_file_path)
    print(f"Excel file saved successfully at: {excel_file_path}")
    
    # Launch Excel on macOS
    subprocess.Popen(["open", excel_file_path])

# Make sure your webInfo and mobileInfo functions are defined above this

How to Verify & Find the File

  • When you run the script, it will print the full path to your random.xls file — you can copy this path and paste it into Finder's "Go > Go to Folder" to open it directly.
  • Alternatively, check the folder where you executed the Python script (use pwd in Terminal to see this folder's path).

Extra Tips

  • If you want to use a temporary file properly (instead of a persistent random.xls), consider using the tempfile module to create a temporary file path that macOS can handle.
  • Ensure you have the correct ChromeDriver installed for macOS (matching your Chrome browser version) — though you mentioned the browser runs until close(), this is always a good check if you run into issues later.

内容的提问来源于stack exchange,提问作者Chae

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:56:23