简体   繁体   English

使用 Openpyxl 打开文件夹中的文件

[英]Opening files in a folder with Openpyxl

I am currently trying to open the files in a folder with the below:我目前正在尝试使用以下内容打开文件夹中的文件:

from tkinter import filedialog
import tkinter as tk
import openpyxl
import os

root = tk.Tk()
root.withdraw()

folder = filedialog.askdirectory()

for f in os.listdir(folder):
    wb = openpyxl.load_workbook(f)
    ws = wb.active
    v = ws['A1']
    print(v.value)

After running this, I run into errors.运行此之后,我遇到了错误。 The value for 'f' is 'filename.xlsx' but does not include the full file path so the file cannot be opened. 'f' 的值为 'filename.xlsx' 但不包括完整的文件路径,因此无法打开文件。 Is there a way to add the rest of the path so that openpyxl can recognize the files?有没有办法添加路径的rest,以便openpyxl可以识别文件? Is there anything else I should change?还有什么我应该改变的吗?

Just found the answer using this guide刚刚使用本指南找到了答案

from tkinter import filedialog
import tkinter as tk
import openpyxl
import os

root = tk.Tk()
root.withdraw()

folder = filedialog.askdirectory()

for f in os.listdir(folder):
    path = os.path.join(folder,f)
    wb = openpyxl.load_workbook(path)
    ws = wb.active
    v = ws['A1']
    print(v.value)

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

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