I trying to get openpyxl to copy and paste rows to a new excel spreadsheet. The only rows that need to get copy and pasted contain the value "move" in the B column.
There are many ways to do it so the example it not specific to what you are doing. To get your data in to the dataframe documentation .
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.