简体   繁体   English

得到错误; attributeerror:'Worksheet'对象没有属性'delete_rows'openpyxl

[英]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.

相关问题 gspread AttributeError: &#39;Worksheet&#39; 对象没有属性 &#39;insert_rows&#39; - gspread AttributeError: 'Worksheet' object has no attribute 'insert_rows' AttributeError:“工作表”对象在openpyxl(python)中没有属性“ get_highest_row” - AttributeError: 'Worksheet' object has no attribute 'get_highest_row' in openpyxl(python) Openpyxl:“工作表”对象没有属性“值” - Openpyxl: 'Worksheet' object has no attribute 'values' Python delete_rows 函数中的 openpyxl 会破坏合并的单元格 - openpyxl in Python delete_rows function breaks the merged cell openpyxl&#39;工作表&#39;对象没有属性&#39;写&#39;(python) - openpyxl 'Worksheet' object has no attribute 'write'(python) Openpyxl:AttributeError:“工作簿”对象没有属性“单元格” - Openpyxl: AttributeError: 'Workbook' object has no attribute 'cell' Openpyxl - AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;lower&#39; - Openpyxl - AttributeError: 'NoneType' object has no attribute 'lower' “ AttributeError:“工作表”对象没有属性“工作表””是什么意思? - What does “AttributeError: 'Worksheet' object has no attribute 'worksheet' ” mean? Openpyxl,max_highest_column给出错误。 工作表对象没有属性“ max_highest_column”? - Openpyxl, max_highest_column gives an error. Worksheet object has no attribute 'max_highest_column'? Python openpyxl模块说:AttributeError:&#39;tuple&#39;对象没有属性&#39;upper&#39; - Python openpyxl module says: AttributeError: 'tuple' object has no attribute 'upper'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM