当我第一次进入文件夹 G:\My Drive\Chip data 时,我可以从该文件夹中检索 Excel 工作表的所有名称。但是,当我尝试使用与之前相同的目录来使用每个文件的名称来打开每个 Excel 工作表时,我收到错误 filenotfound。如有任何帮助,我们将不胜感激!
import os
import glob
import xlwings as xw
import numpy as np
import matplotlib.pyplot as plt
import array
import math
def Excel_Data():
excels = []
app = xw.apps.active
for root, dirs, files in os.walk(r'G:\My Drive\Chip data'):
for file in files:
if file.endswith('.xlsx') and not file.startswith('~'):
file = file[:-5]
excels.append(file)
dir = r'G:\My Drive\Chip data'
for i in excels:
xw.Book(os.path.join(dir, i))
尝试这样:
import os
import xlwings as xw
def Excel_Data():
excels = []
app = xw.apps.active
for root, dirs, files in os.walk(r'C:\Chip data'):
for file in files:
if file.endswith('.xlsx') and not file.startswith('~'):
file = file[:-5]
excels.append(file)
return excels
dir = r'C:\Chip data'
excels = Excel_Data()
for i in excels:
xw.Book(os.path.join(dir, str(i)+".xlsx"))
print(excels) # output: [1, 2]
您正在使用
xw.Book(os.path.join(dir, i))
打开文件,但该文件没有“xlsx”扩展名。
如果您从 Google 云端硬盘而不是计算机上的文件夹打开它,则可能存在另一个问题。使用api插入