[英]getting the error; attributeerror: 'Worksheet' object has no attribute 'delete_rows' openpyxl
i'm writing code for a too to perform GIS functions to an input of an excel sheet. 我正在编写代码以执行Excel功能到excel表的输入。 sometimes the excel sheet will come in and have 2 separate rows across the top for its attributes fields, and when there is 2, I need to delete the top row. 有时excel表会进入并在顶部有2个单独的行作为其属性字段,当有2时,我需要删除顶行。 the value of cell A1 will be naming if I need to do this 如果我需要这样做,单元格A1的值将命名
I tried writing code to check this and delete it as below; 我尝试编写代码来检查并删除它,如下所示;
openpyxl
import arcpy, os, sys, csv, openpyxl
from arcpy import env
env.workspace = r"C:\Users\myname\Desktop\Yanko's tool"
arcpy.env.overwriteOutput = True
excel = r"C:\Users\myname\Desktop\Yanko's tool\Yanko's Duplicate tool\Construction_table_Example.xlsx"
layer = r"C:\Users\myname\Desktop\Yanko's tool\Yanko's Duplicate tool\Example_Polygons.shp"
output = r"C:\Users\myname\Desktop\Yanko's tool\\Yanko's Duplicate tool"
book = openpyxl.load_workbook(excel)
book.get_sheet_by_name("Construction Table format")
if ws.cell(row=1, column=1).value == "Naming":
ws.delete_rows(1, 1)
book.save
book.close
it should just delete the first row if the if function passes true, but I get the error; 如果if函数传递为true,它应该只删除第一行,但是我得到错误;
Warning 警告
(from warnings module):
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\openpyxl\reader\worksheet.py", line 310
warn(msg)
UserWarning: Data Validation extension is not supported and will be removed
Traceback (most recent call last):
File "C:\Users\ronan.corrigan\Desktop\Yanko's tool\Yanko's Duplicate tool\Yanko's Tool.py", line 31, in <module>
ws.delete_rows(1, 1)
AttributeError: 'Worksheet' object has no attribute 'delete_rows'
any help in figuring out what I've done wrong would be greatly appreciated 任何帮助弄清楚我做错了什么都会非常感激
thanks 谢谢
First of all, according to the docs , the get_sheet_by_name
function is deprecated, and you should just be using the sheet name to get the function: 首先,根据文档 ,不推荐使用get_sheet_by_name
函数,您应该只使用工作表名称来获取函数:
book["Construction Table format"]
Another thing to note, in your code I don't see you setting that ws
value, which should be set to whatever sheet object is returned. 另外需要注意的是,在你的代码中,我没有看到你设置ws
值,应该将其设置为返回的任何工作表对象。 If you're setting it somewhere else, so it may be possible that you are using a different sheet object which doesn't have that function 如果您将其设置在其他位置,那么您可能正在使用不具有该功能的不同工作表对象
ws=book["Construction Table format"]
Other than that you'd have to share the stack trace to give a better understanding of what's breaking 除此之外,你必须分享堆栈跟踪,以便更好地了解什么是破坏
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.