I have 6 work sheets in my workbook. I want to copy data (all used cells except the header) from 5 worksheets and paste them into the 1st. Snippet of code that applies:
`
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(mergedXL)
wsSIR = wb.Sheets(1)
sheetList = wb.Sheets
for ws in sheetList:
used = ws.UsedRange
if ws.Name != "1st sheet":
print ("Copying cells from "+ws.Name)
used.Copy()
`
used.Copy() will copy ALL used cells, however I don't want the first row from any of the worksheets. I want to be able to copy from each sheet and paste it into the first blank row in the 1st sheet. So when cells from the first sheet (that is NOT the sheet I want to copy to) are pasted in the 1st sheet, they will be pasted starting in A3. Every subsequent paste needs to happen in the first available blank row. I probably haven't done a great job of explaining this, but would love some help. Haven't worked with win32com a ton.
I also have this code from one of my old scripts, but I don't understand exactly how it's copying stuff and how I can modify it to work for me this time around:
ws.Range(ws.Cells(1,1),ws.Cells(ws.UsedRange.Rows.Count,ws.UsedRange.Columns.Count)).Copy()
wsNew.Paste(wsNew.Cells(wsNew.UsedRange.Rows.Count,1))
If I understand well your problem, I think this code will do the job:
import win32com.client
# create an instance of Excel
excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
# Open the workbook
file_name = 'path_to_your\file.xlsx'
wb = excel.Workbooks.Open(file_name)
# Select the first sheet on which you want to write your data from the other sheets
ws_paste = wb.Sheets('Sheet1')
# Loop over all the sheets
for ws in wb.Sheets:
if ws.Name != 'Sheet1': # Not the first sheet
used_range = ws.UsedRange.SpecialCells(11) # 11 = xlCellTypeLastCell from VBA Range.SpecialCells Method
# With used_range.Row and used_range.Col you get the number of row and col in your range
# Copy the Range from the cell A2 to the last row/col
ws.Range("A2", ws.Cells(used_range.Row, used_range.Column)).Copy()
# Get the last row used in your first sheet
# NOTE: +1 to go to the next line to not overlapse
row_copy = ws_paste.UsedRange.SpecialCells(11).Row + 1
# Paste on the first sheet starting the first empty row and column A(1)
ws_paste.Paste(ws_paste.Cells(row_copy, 1))
# Save and close the workbook
wb.Save()
wb.Close()
# Quit excel instance
excel.Quit()
I hope it helps you to understand your old code as well.
Have you considered using pandas?
import pandas as pd
# create list of panda dataframes for each sheet (data starts ar E6
dfs=[pd.read_excel("source.xlsx",sheet_name=n,skiprows=5,usecols="E:J") for n in range(0,4)]
# concatenate the dataframes
df=pd.concat(dfs)
# write the dataframe to another spreadsheet
writer = pd.ExcelWriter('merged.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()
Related
I'm trying copy and paste some data from one sheet to another sheet. The code works fine but I only need the value.
original_wb = xl.load_workbook(filename1)
copy_to_wb = xl.load_workbook(filename1)
source_sheet = original_wb.worksheets[0] # The first worksheet
copy_to_sheet = copy_to_wb.create_sheet(source_sheet.title+"_copy")
for row in source_sheet:
for cell in row:
copy_to_sheet[cell.coordinate].value = cell.value
copy_to_wb.save(str(filename1))
Can this be done in pandas instead?
if you want just values to be read and copied to new sheet . try read excel and write excel commands.
file_name= r"path"
#Read
df= (pd.read_excel(io=file_name,sheet_name='name'))
#process required data
#write to new work book or sheet
df.to_excel( file_name ,sheet_name= 'name')
I'm comparing two workbooks using Openpyxl I have it incrementing a counter for later usage and then keeping track of rows that should be removed from the initial workbook. How do I go about getting rid of these rows from that workbook or creating a new sheet(With the Original then deleted) or workbook with those rows removed?
I've written the code up until this point but I havent found much in terms of writing or deleting rows from a workbook and I haven't any concrete luck, I was advised by someone to instead create a copy of the workbook but I also have had no success at doing such.
from openpyxl import load_workbook
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import datetime
import time
class ManualReporter:
def __init__(self):
'''
Initializes Variables for use within the Class
Hides the tkinter pop-up when using the file dialog
'''
Tk().withdraw()
self.sap_file = None
self.tracker_file = None
self.wb_sap = None
self.wb_wt = None
self.XT = 0
self.deadrows = []
def open_sapfile(self):
'''
Sets the sap_file variable to be the first directory to the SAP Report based on what the User Selects in the File Dialog
Sets that directory and the file as the current workbook under the variable self.wb_sap
Creates a Backup of the SAP Report so that if Errors Occur a Fresh Clean Copy is Present
'''
self.sap_file = askopenfilename()
self.wb_sap = load_workbook(filename=self.sap_file)
# Code to create a backup File in-case of Error or Fault
copyfile = "Untimed_Report_SAP_" + str(datetime.date.today())+".xlsx"
self.wb_sap.save(copyfile)
print(self.sap_file)
def open_tracker(self):
'''
Same as Above, sets self.tracker_file as a filedialog which retrieves the file's directory (User Inputted)
Loads the File Workbook as self.wb_wt
Creates a Backup of the Second SAP Report so that if Error Occurs a Clean Copy is Present.
'''
self.tracker_file = askopenfilename()
self.wb_wt = load_workbook(filename=self.tracker_file)
print(self.tracker_file)
def check_rows(self):
'''
Sets the Active Sheets in Both the Workbook Variables,
Creates a New Sheet in the Newest Report to Contain the Modified Data,
Iterates through the Rows of the Two Sheets checking for a Comparison in Part Number,
OpCode and then Compares the X/T/P Classification and Adjusts Data in Second Sheet
'''
start = time.time()
sap = self.wb_sap.worksheets[0] #Sets The First Sheet in the Excel Workbook as the variable sap
wt = self.wb_wt.worksheets[0]#Sets the First Sheet in the Second Report as the var wt
ws1 = self.wb_sap.create_sheet("Sheet1", 1)#Create a Spare Sheet in the First Report to place the Adjusted Data
ws1 = self.wb_sap.worksheets[1]#Sets ws1 as the Active Second Sheet for New Data
for saprow in sap.iter_rows():
for wtrow in wt.iter_rows():
if (saprow[3].value == wtrow[4].value and int(saprow[2].value) == int(wtrow[5].value)):# IF Material NUM & OPCode MATCH DO:
if wtrow[7].value in ("T","P"): #WT Entry is Marked as T/P
if saprow[4].value is "X": #SAP Report Entry is Marked as X
self.XT += 1#Increment X->Ts Counts
#print("X->T")
self.deadrows.append(saprow)
else:
if saprow not in self.deadrows:
ws1.append(saprow)
end = time.time()
#print("Finished, Total X->Ts: ", self.XT)
print("Time Taken: ", (end - start))
x = ManualReporter()
x.open_sapfile()
x.open_tracker()
x.check_rows()
My expectation is that the output would be an exact copy of workbook one but the rows that had a certain change in values are removed from that workbook. I expected to be able to delete them but no methods I've done have achieved anything other than broken code or issues.
self.deadrows.append(saprow)
else:
if saprow not in self.deadrows:
for i in saprow:
#Code to Create a row in ws1.
#Code to Append value of saprow[i] to current ws1 rows
EDIT 1: I included my Attempts to append the rows to a copied worksheet.
EDIT 2: I though about manually iterating through the Saprow and appending the data into the rows of the new sheet but I've stumped myself thinking about it.
After Ample help I have reached the conclusion that to copy data from one sheet to another you can copy over data row by row through this Method:
self.workbook = load_workbook(filename="filepath")
sheet1 = self.workbook.worksheet[0]
sheet2 = self.workbook.create_sheet("Sheet 2")
sheet2 = self.workbook.worksheets[1]
for row in sheet1.iter_rows():
sheet2.append([cell.value for cell in row])
I also figured out if you want to filter out data you can add if statements inside of the for-loop above that can limit what rows have their cells written into the new worksheet.
self.RowsToExclude = Some List containing row data that will be excluded.
for row in sheet1.iter_rows():
if row not in self.RowsToExclude:
ws1.append([cell.value for cell in row])
Finally, I'd like to thank all those who contributed towards me reaching this conclusion.
i have to update/append data into existing xlsx file.
xlsx file contains multiple sheets.
for example i want to append some data into existing sheet 'Sheet1', how to do this
To append a new row of data to an existing spreadsheet, you could use the openpyxl module. This will:
Load the existing workbook from the file.
Determines the last row that is in use using ws.get_highest_row()
Add the new row on the next empty row.
Write the updated spreadsheet back to the file
For example:
import openpyxl
file = 'input.xlsx'
new_row = ['data1', 'data2', 'data3', 'data4']
wb = openpyxl.load_workbook(filename=file)
ws = wb['Sheet1'] # Older method was .get_sheet_by_name('Sheet1')
row = ws.get_highest_row() + 1
for col, entry in enumerate(new_row, start=1):
ws.cell(row=row, column=col, value=entry)
wb.save(file)
Note, as can be seen in the docs for XlsxWriter:
XlsxWriter is designed only as a file writer. It cannot read or modify
an existing Excel file.
This approach does not require the use of Windows / Excel to be installed but does have some limitations as to the level of support.
Try xlwings (currently available from http://xlwings.org) it is suitable for both reading and writing excel files.
Everything you need is in the quickstart tutorial. Something like this should be what you want.
import xlwings as xw
with open("FileName.xlsx", "w") as file:
wb = xw.Book(file) # Creates a connection with workbook
xw.Range('A1:D1').value = [1,2,3,4]
Selecting a Sheet
In order to read and write data to a specific sheet. You can activate a sheet and then call Range('cell_ref').
Sheet('Sheet1').activate();
Using Range to select cells
To select a single cell on the current worksheet
a = xw.Range('A1').value;
xw.Range('A1').value = float(a)+5;
To explicitly select a range of cells
xw.Range('A1:E8').value = [new_cell_values_as_list_of_lists];
xw.Range('Named range').value = [new_cell_values_as_list_of_lists];
To automatically select a contiguous range of populated cells that start from 'A1' and go right and down... until empty cell found.
Range('A1').table.value;
It is also possible to just select a row or column using:
Range('A1').vertical.value;
Range('A1').horizontal.value;
Other methods of creating a range object (from the api doc enter link description here)
Range('A1') Range('Sheet1', 'A1') Range(1, 'A1')
Range('A1:C3') Range('Sheet1', 'A1:C3') Range(1, 'A1:C3')
Range((1,2)) Range('Sheet1, (1,2)) Range(1, (1,2))
Range((1,1), (3,3)) Range('Sheet1', (1,1), (3,3)) Range(1, (1,1), (3,3))
Range('NamedRange') Range('Sheet1', 'NamedRange') Range(1, 'NamedRange')
im trying to copy all cells on a sheet to a new workbook. i can store cell values manually like in the example code below and paste variable in respective cells but i want to automate the collection of cell data. I am very new to python but i can conceptually see something along the line of this but i could use some help to finish it, thanks!
attempt to automate cell collection
def cell(r,c):
set r+=1
cellname = c.isalpha() + r
if r <= sheet.nrow:
cellname = (r,c,sheet.cell_value)
...... i get lost around here but i assume there should be a sheet.ncols and nrows
current manual cell copying
cellA1 = sheet.cell_value(0,0)
cellA2 = sheet.cell_value(1,0)
cellA3 = sheet.cell_value(2,0)
cellA4 = sheet.cell_value(3,0)
cellA5 = sheet.cell_value(4,0)
cellB1 = sheet.cell_value(0,1)
cellB2 = sheet.cell_value(1,1)
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('ITEM DETAILS')
manual cell pasting
sheet.write(0, 0, cellA1)
sheet.write(1, 0, cellA2)
You can just simply loop through the cells in the sheet, by using sheet.nrows and sheet.ncols as the limit to loop up to. Also, make sure you do not define the new worksheet you are creating as sheet itself, use a new name. Example:
newworkbook = xlwt.Workbook()
newsheet = newworkbook.add_sheet('ITEM DETAILS')
for r in range(sheet.nrows):
for c in range(sheet.ncols):
newsheet.write(r, c, sheet.cell_value(r, c))
Then use newsheet instead of sheet wherever you want to use the new sheet.
Anand S Kumar's answer is correct but you need to change i to r and j to c. For extra benefit I added a bit more code for a complete code example. This code opens an existing excel file, reads all of the data from the first sheet, and writes that same data to a new excel file.
import os,xlrd,xlwt
if os.path.isfile(outExcel):os.remove(outExcel)#delete file if it exists
inExcel= (r'C:\yourpath\inFile.xls')
outExcel= (r'C:\yourpath\outFile.xls')
workbook = xlrd.open_workbook(inExcel)
sheetIn = workbook.sheet_by_index(0)
workbook = xlwt.Workbook()
sheetOut = workbook.add_sheet('DATA')
for r in range(sheetIn.nrows):
for c in range(sheetIn.ncols):
sheetOut.write(r, c, sheetIn.cell_value(r, c))
workbook.save(outExcel)#save the result
I am trying to open an excel file then insert data into the 2nd
row (pushing all existing data below the 1st row down by one row). I don't want to overwrite any existing data, just add the new data and push the existing data down by one row.
Here is some code I'm using to get me started:
import xlrd
import xlwt
from xlutils.copy import copy
def save_test_log(test_log_path, selected_save_path, test_type, date_n_time, tester):
rb = xlrd.open_workbook(test_log_path,formatting_info=True)
r_sheet = rb.sheet_by_index(0) # sets r_sheet to be the 1st sheet
r = r_sheet.nrows # gets the number of rows in the 1st sheet.
wb = copy(rb) # coppies contents of rb to a write file so we can edit it.
sheet = wb.get_sheet(0) # select the 1st sheet in wb
This is about as far as I get. I know how to add rows to the end of the sheet
but I have no idea how to "insert" rows of existing data into the sheet.
Do you have the answers I seek?
Thanks,