简体   繁体   English

为什么我不能用 openpyxl 编辑我的 .xlsx 文件?

[英]Why can't I edit my .xlsx file with openpyxl?

I am encountering a problem when running with openpyxl the code below我在使用 openpyxl 下面的代码运行时遇到问题

import openpyxl
import os

wb = openpyxl.load_workbook('example.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
sheet["A1"].value
sheet["A1"].value == None
sheet["A1"].value = 42
sheet["A3"].value = 'Hello'
os.chdir("/Users/mac/Desktop")
wb.save('exceeeel.xlsx')

The error is错误是

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 312, in load_wo
rkbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 124, in __init_
_
    self.archive = _validate_archive(fn)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 96, in _validat
e_archive

What am I doing wrong?我究竟做错了什么? I am using the current version of the openpyxl library.我正在使用 openpyxl 库的当前版本。

I can't provide a confident answer because the question only includes a partial traceback.我无法提供自信的答案,因为该问题仅包含部分追溯。 That being said, it looks like the traceback one would get for a FileNotFoundError :话虽如此,看起来FileNotFoundError会得到回溯:

C:\Python37\python.exe C:/Users/user/PycharmProjects/scratch/scratch2.py
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/scratch/scratch2.py", line 3, in <module>
    wb = openpyxl.load_workbook('example.xlsx')
  File "C:\Python37\lib\site-packages\openpyxl\reader\excel.py", line 313, in load_workbook
    data_only, keep_links)
  File "C:\Python37\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "C:\Python37\lib\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "C:\Python37\lib\zipfile.py", line 1207, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'example.xlsx'

Process finished with exit code 1

This error will be raised when the path your provided to the file you want to load with openpyxl.load_workbook does not contain the specified file.当您提供的要使用openpyxl.load_workbook加载的文件的路径不包含指定的文件时,将引发此错误。 Since the only argument you provided in your call of that function is 'example.xlsx' that probably means there is no file in the folder you are running this script from.由于您在调用该函数时提供的唯一参数是'example.xlsx' ,这可能意味着您运行此脚本的文件夹中没有文件。

If this 'example.xlsx' file is in a different folder then you'll want to either specify the relative path to that file as your argument or move the file into the same folder as your script.如果此'example.xlsx'文件位于不同的文件夹中,则您需要指定该文件的相对路径作为参数,或者将文件移动到与脚本相同的文件夹中。

If this isn't what's going on then you'll need to provide the full traceback that you are seeing on your end in order to get a better answer.如果这不是正在发生的事情,那么您需要提供您在最后看到的完整回溯,以便获得更好的答案。

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

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