簡體   English   中英

使用pywin32查找Excel工作簿

[英]Using pywin32 to find an Excel Workbook

我已經下載並安裝了pywin32。 我正在嘗試使用pywin32打開工作簿,我幾乎可以肯定我正在正確地對此進行編碼,但是我遇到了這樣的錯誤。

   File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u"'Kithenshiftweekly.xlsx' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.", u'xlmain11.chm', 0, -2146827284), None)

我當前的代碼如下(不是很多)

import win32com.client
x1 = win32com.client.Dispatch("Excel.Application")
x1.Visible = True
x1.Workbooks.Open("Kitchen")

x1.Workbooks.Open(“ Kitchen”)導致此問題。

但這找不到工作簿。 發生了什么?

有兩個問題。 首先,您必須附加擴展名(例如“ Kitchen.xlsx”),其次,您必須提供工作簿的完整路徑。 因此,如果您的工作簿為“ Kitchen.xlsx”,並且您的腳本位於同一文件夾中,則您的代碼應類似於以下內容:

import win32com.client
import os
x1 = win32com.client.Dispatch("Excel.Application")
x1.Visible = True
x1.Workbooks.Open(os.path.join(os.getcwd(), "Kitchen.xlsx"))

(os.getcwd()返回腳本目錄的完整路徑。如果要將代碼和工作簿存儲在其他目錄中,則可以通過字符串指定路徑。打印os.getcwd()來了解如何要做到這一點。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM