繁体   English   中英

使用 openpyxl 删除在 excel 工作表的 coulmn A 中找到的值以上的所有行

[英]Deleting all rows above a value found in coulmn A of an excel sheet with openpyxl

我是 Python 和编码的新手,决定开始一个项目来学习。 代码将原始文件复制并粘贴到一个新工作簿中(原始文件受作者保护)接下来我想发生的是删除“位置:001 一般注释”上方的每一行。 运行脚本时我没有收到任何错误,但没有任何内容被删除。 我用来删除行的代码是我在此处找到的代码。

import openpyxl as xl
#Creates new workbook, saves it over the original, removes design
path1 = "/home/user/Delavue/84-22-1037 Piedmont Model Takeoff.xlsx"

wb1 = xl.load_workbook(filename=path1)
ws1 = wb1.worksheets[0]

wb2 = xl.Workbook()
ws2 = wb2.create_sheet(ws1.title)

for row in ws1:
    for cell in row:
        ws2[cell.coordinate].value=cell.value
del wb2['Sheet']
#Find location 001 and delete everything above it
mr1=ws2.max_row
mc1=ws2.max_column
for j in range(1,mr1+1):
    for i in range(1,mc1+1):
        if ws2.cell(row=i,column=j).value=="Location : 001 General Notes":
            if i > 1:
                ws2.delete_rows(1, i-1)
            break
wb2.save(path1)


以下是文件外观的片段:

图像

我现在正试图删除“第一个位置”上方的所有行。 第一个位置总是以“位置:”开头,但每次后面都有不同的东西,所以我将无法使用 ==

我输入了以下内容,它运行时没有任何错误,但它什么也没做。 我只需要它删除第一个位置然后停止。

图片: https://i.stack.imgur.com/kA8Gj.png

#Creates new workbook, saves it over the original, removes design
path1 = "/home/sean-desmet/Delavue/84-22-1037 Piedmont Model Takeoff.xlsx"

wb1 = xl.load_workbook(filename=path1)
ws1 = wb1.worksheets[0]

wb2 = xl.Workbook()
ws2 = wb2.create_sheet(ws1.title)

for row in ws1:
    for cell in row:
        ws2[cell.coordinate].value=cell.value
del wb2['Sheet']

#Find location 001 and delete everything above it

mr1=ws2.max_row
mc1=ws2.max_column
for j in range(1,mc1+1):
    for i in range(1,mr1+1):
        if ws2.cell(row=i,column=j).value=="Location : 001 General Notes":
            if i > 1:
                ws2.delete_rows(1, i)
            break
        else:
            continue
        break
#Go to correct location and delete rows above it
mr2=ws2.max_row
mc2=ws2.max_column
for n in range(1,mc2+1):
    for m in range(1,mr2+1):
        if cell.value is not None:
            if type(cell.value) != int:
                if 'Location : ' in ws2.cell(row=m,column=n).value:
                    if m > 1:
                        ws2.delete_rows(1, m)
                    break
                else:
                    continue
                break
wb2.save(path1)`


查看您的第一个代码示例;
由于工作表保护,您需要将数据从工作簿 1 复制到工作簿 2,因此只复制您需要的内容而不是复制所有内容然后删除不需要的内容是有意义的。

考虑到这一点,请看下面的代码示例。
首先,我们检查您要保留的文本的开头位置,它似乎是字符串"Location: 001 General Notes" ,然后仅从该行开始复制数据。
该代码示例仅在 A 列中搜索搜索文本。 线路

ws1.iter_rows(max_col=1)

将仅循环通过 Coulumn A。 当找到文本时,代码会跳出该循环并从该行开始另一个循环,这次遍历所有使用的列。

for row in ws1.iter_rows(min_row=copy_start_row):

copy_start_row是找到搜索文本的行。 ws1 当前坐标处的单元格值被复制到 ws2 的第一行同一列,依此类推到所用数据的末尾。
结果是现有工作表的副本,ws1 上的数据从第 10 行开始,从 ws2 上的第 1 行开始。

import openpyxl as xl

# Creates new workbook, saves it over the original, removes design
path1 = "/home/sean-desmet/Delavue/84-22-1037 Piedmont Model Takeoff.xlsx"

wb1 = xl.load_workbook(filename=path1)
ws1 = wb1.worksheets[0]

wb2 = xl.Workbook()
ws2 = wb2.active
ws2.title = ws1.title

search_text = "Location : 001 General Notes"

copy_start_row = 1  # Variable for the first row to copy data from
for row in ws1.iter_rows(max_col=1):
    c_val = row[0].value
    if c_val is not None and c_val in search_text:
        print(f'Cell with the value is {row[0].coordinate}')
        copy_start_row = row[0].row
        break  # break from loop when search text found

row_offset = copy_start_row-1  # row offset for location of data in ws2
for row in ws1.iter_rows(min_row=copy_start_row):
    for cell in row:
        ws2.cell(row=cell.row-row_offset,column=cell.column).value = ws1[cell.coordinate].value

wb2.save(path1)  # Possibly want to use a different file name

暂无
暂无

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

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