使用OpenPyXl节省工作簿的更改

发布于 2025-02-10 16:31:09 字数 2321 浏览 1 评论 0原文

来自应用程序的Excel表导出将空白列和行添加到输出数据。我正在尝试在按摩数据之前将其删除。

因此,我有一些简单的代码来删除前3行和前2个Columsn,然后保存更改。

当我使用workbook.save(路径)时,我会收到追踪错误。我研究了Stackoverflow,看起来这个简单的代码应该可以正常工作。我什至尝试过将文件名放置,但也会出现类似的错误。

我不确定为什么要遇到这个错误。

更新:我更正了typo workbook.save(路径)为workbook.save(path)。

import openpyxl
import pandas as pd
from openpyxl import Workbook

# Give the location of the file
path = 'C:\\Users\\cbout\\Desktop\\2022 Data.xlsx'
filename = '2022 Data.xlsx'

DF = pd.read_excel(path, sheet_name=1)

# # To open the workbook
# # workbook object is created
workbook = openpyxl.load_workbook(path)

# # Get workbook active sheet objectmfrom the active attribute
worksheet = workbook.active

#Delete first 2 columns.
worksheet.delete_cols(0,2)
#Delete first 3 rows.
worksheet.delete_rows(0,4)

workbook.save(path)

更新后新的回溯错误

PS C:\Users\cbout\Documents\GitHub> & "C:/Program Files/Python310/python.exe" c:/Users/cbout/Documents/GitHub/Python/Stewardship.py
Traceback (most recent call last):
  File "c:\Users\cbout\Documents\GitHub\Python\Stewardship.py", line 9, in <module>
    stewardDF = pd.read_excel(path, sheet_name=1)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_base.py", line 1385, in __init__
    engine = config.get_option(f"io.excel.{ext}.reader", silent=True)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\_config\config.py", line 256, in __call__
    return self.__func__(*args, **kwds)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\_config\config.py", line 128, in _get_option
    key = _get_single_key(pat, silent)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\_config\config.py", line 114, in _get_single_key
    raise OptionError(f"No such keys(s): {repr(pat)}")
pandas._config.config.OptionError: No such keys(s): 'io.excel.zip.reader'

An excel sheet export from an app adds blank columns and rows to the output data. I'm trying to remove it before massaging the data.

So I have some simple code to delete the first 3 rows and first 2 columsn, then save the changes.

When I use Workbook.save(path) I get a traceback error. I researched on stackoverflow and it looks like this simple code should be working. I've even tried just putting the filename but that also giving similar error.

I'm not sure why I'm getting this error.

Update: I corrected the typo Workbook.save(path) to workbook.save(path).

import openpyxl
import pandas as pd
from openpyxl import Workbook

# Give the location of the file
path = 'C:\\Users\\cbout\\Desktop\\2022 Data.xlsx'
filename = '2022 Data.xlsx'

DF = pd.read_excel(path, sheet_name=1)

# # To open the workbook
# # workbook object is created
workbook = openpyxl.load_workbook(path)

# # Get workbook active sheet objectmfrom the active attribute
worksheet = workbook.active

#Delete first 2 columns.
worksheet.delete_cols(0,2)
#Delete first 3 rows.
worksheet.delete_rows(0,4)

workbook.save(path)

New Traceback Error after updates

PS C:\Users\cbout\Documents\GitHub> & "C:/Program Files/Python310/python.exe" c:/Users/cbout/Documents/GitHub/Python/Stewardship.py
Traceback (most recent call last):
  File "c:\Users\cbout\Documents\GitHub\Python\Stewardship.py", line 9, in <module>
    stewardDF = pd.read_excel(path, sheet_name=1)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_base.py", line 1385, in __init__
    engine = config.get_option(f"io.excel.{ext}.reader", silent=True)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\_config\config.py", line 256, in __call__
    return self.__func__(*args, **kwds)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\_config\config.py", line 128, in _get_option
    key = _get_single_key(pat, silent)
  File "C:\Users\cbout\AppData\Roaming\Python\Python310\site-packages\pandas\_config\config.py", line 114, in _get_single_key
    raise OptionError(f"No such keys(s): {repr(pat)}")
pandas._config.config.OptionError: No such keys(s): 'io.excel.zip.reader'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文