首页 > 解决方案 > 如何使用xlwings Python同时打开3个excel文件?

问题描述

我正在尝试打开某个文件夹中的所有 .xlsx 文件。我使用了 xlwings 模块。肯定最后一行代码不正确。你能给我一个提示吗?

import os
import glob
import xlwings as xw
excels = []
for root, dirs, files in os.walk(r'C:\Users\ego\PycharmProjects\TRIAL01\excel files'):
    for file in files:
        if file.endswith('.xlsx'):
            excels.append(file)
for i in excels:
    xw.Book(r'C:\Users\ego\PycharmProjects\TRIAL01\excel files', i)

标签: pythonexcelxlwings

解决方案


这里有一个提示:使用 os.path.join() 方法,因为您当前的字符串将显示为 'C:\Users\ego\PycharmProjects\TRIAL01\excel filesdoc.xlsx' 并且缺少连接反斜杠。

dir = r'C:\Users\ego\PycharmProjects\TRIAL01\excel files'
for i in excels:
    xw.Book(os.path.join(dir, i))

推荐阅读