简体   繁体   English

使用 Python 完成多个 Excel 工作簿的文件路径

[英]Complete file path of multiple Excel workbooks with Python

I have multiple Excel workbooks open on my computer, which are in different instances.我的计算机上打开了多个 Excel 工作簿,它们位于不同的实例中。 I am using the following script to get the complete path of all the files.我正在使用以下脚本来获取所有文件的完整路径。

With below code I can get only a list of file names.使用下面的代码,我只能得到一个文件名列表。 How can I get the full path?我怎样才能得到完整的路径?

import xlwings as xw
wb = xw.books
print (wb)

You can get the full path of opened(active) excel file by using this:您可以使用以下命令获取已打开(活动)excel 文件的完整路径:
If you want then here's the docs , you can explore other options also from docs.如果您愿意,那么这里是docs ,您也可以从 docs 探索其他选项。

import xlwings as xw
wb = xw.books.active
print(wb.fullname)
"""
C:\Users\...\Desktop\Book1.xlsx
"""

But if you want to get path of all files then:但是如果你想获取所有文件的路径,那么:

import xlwings as xw
wb= xw.books
for w in wb:
    print(w.fullname)
"""
C:\Users\...\Desktop\Book1.xlsx
C:\Users\...\Desktop\Book2.xlsx
C:\Users\...\Desktop\Book3.xlsx
"""

Each Book object has a fullname property:每个Book对象都有一个fullname属性:

import xlwings

workbooks = xlwings.books
for workbook in workbooks:
    print(workbook.fullname)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM