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.
Asked
Active
Viewed 137 times
-4
-
It sound like you want others to do your research. I recommend that you use pandas to filter the data. For openpyxl look at this... https://stackoverflow.com/questions/36657288/copy-pandas-dataframe-to-excel-using-openpyxl – Shane S Dec 05 '22 at 18:44
-
I have been searching all over, I came here to ask a question to know if anyone had the answer. I wasn't asking anyone to research for me. – irishombian Dec 05 '22 at 18:48
-
I think of openpyxl as a tool to format data that is in you pandas dataframe or preserve formatting in the file. here is another example. https://stackoverflow.com/questions/56940231/writing-dataframe-to-excel-sheet-with-openpyxl – Shane S Dec 05 '22 at 18:53
1 Answers
0
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)

Shane S
- 1,747
- 14
- 31
-
Thank you for the attempt however I am getting "Excel xlsx file; not supported" – irishombian Dec 05 '22 at 19:54
-
[look at this link](https://stackoverflow.com/questions/65254535/xlrd-biffh-xlrderror-excel-xlsx-file-not-supported). If that does not help, I recommend that you make a new question that includes your code. – Shane S Dec 05 '22 at 20:01
-
another one. https://stackoverflow.com/questions/65250207/pandas-cannot-open-an-excel-xlsx-file – Shane S Dec 05 '22 at 20:04
-