简体   繁体   English

使用 Python 打开 Excel 工作簿

[英]Use Python to open an Excel workbook

I am learning Python through 'Automate the Boring Stuff With Python' First Edition.我正在通过“使用 Python 自动化无聊的东西”第一版来学习 Python。 In chapter 12, pg 267, we are supposed to open a file called example.xlsx.在第 12 章第 267 页,我们应该打开一个名为 example.xlsx 的文件。

The author's code reads:作者的代码如下:

import openpyxl
wb = openpyxl.load_workbook('example.xlsx')
type(wb)

However, when I try to open this file, I get the following error (this is the last line of the error):但是,当我尝试打开此文件时,出现以下错误(这是错误的最后一行):

 FileNotFoundError: [Errno 2] No such file or directory: 'example.xlsx'

I know this file exists, because I downloaded it myself and am looking at it right now.我知道这个文件存在,因为我自己下载了它,现在正在查看它。

I have tried moving it to the current location in which Python 3.8 is, I have tried saving it with my Automate the Boring Stuff files that I've been working on the desktop, and I have tried saving it in every conceivable location on my machine, but I continue getting this same message.我已经尝试将它移动到 Python 3.8 所在的当前位置,我尝试使用我一直在桌面上工作的 Automate the Boring Stuff 文件保存它,并且我尝试将它保存在我机器上每个可以想象的位置,但我继续收到同样的消息。

I have imported openpyxl without error, but when I enter the line我已经毫无错误地导入了openpyxl,但是当我进入该行时

wb = openpyxl.load_workbook('example.xlsx') 

I have entered the entire pathway for the example.xlsx in the parenthesis, and I continue to get the same error.我已经为括号中的 example.xlsx 输入了整个路径,并且我继续收到相同的错误。

What am I doing wrong?我究竟做错了什么? How am I supposed to open an Excel workbook?我应该如何打开 Excel 工作簿?

I still don't understand how I am doing wrong, but this one is incredibly infuriating, and I feel incredibly stupid, because it must be something simple.我仍然不明白我是怎么做错的,但是这个非常令人愤怒,我觉得非常愚蠢,因为它一定很简单。

Any insight/help is greatly appreciated.非常感谢任何见解/帮助。

Your error is unambigous — your file in a supposed directory don't exist .您的错误是明确的 - 您在假定目录中的文件不存在 Believe me.相信我。

For Python is irrelevant, whether you see it.对于 Python 是无关紧要的,无论看到它。 Python itself must see it. Python 自己必须看到。

Specify the full path, using forward slashes, for example:使用斜杠指定完整路径,例如:

wb = openpyxl.load_workbook('C:/users/John/example.xlsx')

Or find out your real current (working) directory — and not the one supposed by you — with commands或者使用命令找出你真正的当前(工作)目录——而不是你假设的目录

import os
print(os.getcwd())

then move your example.xlsx to it, and then use only the name of your file然后将您的example.xlsx移动到它,然后仅使用您的文件名

wb = openpyxl.load_workbook('example.xlsx')

You may also verify its existence with commands — use copy/paste from your code to avoid typos in the file name / path您还可以使用命令验证它的存在——使用代码中的复制/粘贴来避免文件名/路径中的拼写错误

import os.path
print(os.path.exists('example.xlsx'))                   # True, if Python sees it

or或者

import os.path
print(os.path.exists('C:/users/John/example.xlsx'))     # True, if exists

to be sure that I'm right, ie that the error is not in the function openpyxl.load_workbook() itself, but in its parameter (the path to the file) provided by you .确保我是对的,即错误不在 function openpyxl.load_workbook()本身,而是在提供的参数(文件的路径)中。

I notice that the extension of the example file is not the same as described in the book, is example.csv.我注意到示例文件的扩展名与书中描述的不一样,是example.csv。 I was facing the same frustration as you我面临着和你一样的挫败感

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

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