簡體   English   中英

openpyxl刪除Excel中的Table行

[英]Openpyxl to delete Table row in Excel

我很難弄清楚如何刪除整個空行。 當該行是 Excel 表的一部分時。

所以我嘗試了以下代碼。 但它保留了表格的格式並且它不起作用,即對於像 Count If 這樣的函數,因為它計算那些空白行。

from openpyxl import load_workbook as lw

wb = lw(file)
ws = wb['Sheet']

endrow = 10 #target row from which I will delete

#delete entire rows from endrow to the end of the sheet 
for i in range(endrow, ws.max_row + 1): 
    wsRemesas.delete_rows(i)

我希望這些行像默認文件一樣絕對空白。 不是表格的一部分或格式。

問候。

我使用了這個解決方案:

import xlwings as xw
from xlwings.constants import DeleteShiftDirection

app = xw.App(visible=False)
wb = app.books.open('PathtoFile')
sht = wb.sheets['SheetName']

endrow = XX #number of target row from you want to delete below

# Delete after endrow till row 10,000
sht.range(str(endrow)+':10000').api.Delete(DeleteShiftDirection.xlShiftUp) 

wb.save()
app.kill()  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM