![](/img/trans.png)
[英]Openpyxl: How to copy a row after checking if a cell contains specific value
[英]Copy a row based on a specific cell value openpyxl
我試圖讓 openpyxl 將行復制並粘貼到新的 excel 電子表格中。 唯一需要復制和粘貼的行在 B 列中包含值“move”。
有很多方法可以做到這一點,所以這個例子並不特定於你在做什么。 將您的數據寫入 dataframe 文檔。
import pandas as pd
from openpyxl import load_workbook
path = "C:/path to/your file/example.xlsx"
df = pd.read_excel(path, sheet_name='Sheet1', usecols='A,B,C')
# for demo purposes the column head names are ['A','B','C']
df = df[df['B']=='move']
wb = load_workbook(path, data_only=True)
sh = wb["move"]
# lets have the rows start on line 4 because for what ever reason...
rows = df.shape[0] + 4
for r in range(4, rows):
# df values for the table
i = r - 4
sh.cell(column=1, row=r).value = df["A"].iloc[i]
sh.cell(column=2, row=r).value = df["B"].iloc[i]
sh.cell(column=3, row=r).value = df["C"].iloc[i]
wb.save(path)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.