How to write in existing Excel Sheet

Asked by Prateek Sahu

I already have a Excel sheet,
now I want to write data in exiting excel sheet. I don't want to create new sheet

Help me

Thanks in advance.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Prateek Sahu
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

basic info see comment #1 at question
https://answers.launchpad.net/sikuli/+question/187101

Writing in an existing excel sheet is not possible the easy way.
All the above solutions generate new sheets/files, that have to be merged into the existing sheet afterwards using standard Excel features (which might be controlled with Sikuli GUI manipulations).

You might try to GUI-control an the Excel app and write directly into an existing sheet using paste(), but this might be a gigantic effort not really worth to spend.

I am not an expert, but Office Basic might have interesting features, that might be used and controlled by Sikuli.

Many options, but if I had to do it, I would create a csv file and merge it into the existing Excel sheet afterwards.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

UUUps, why didn't you subscribe to the question?

Revision history for this message
Prateek Sahu (prateek-sahu01apr) said :
#3

I found a solution for my problem Raiman,
I am able to write in a existing Excel sheet using xlutils

Revision history for this message
RaiMan (raimund-hocke) said :
#4

Good finding:
http://pypi.python.org/pypi/xlutils

So with xlrd (read Excel sheets), xlwt (create new Excel files) and xlutils we have all we need for working with Excel content directly from Sikuli.

Revision history for this message
hcuthbert (howard-cuthbert) said (last edit ):
#5

Example 1 -- workbook is already created, column has been added to the worksheet where cell data is added. Data has been added to the clipboard (Ctrl &C -- copy)
from sikuli import *
import xlrd
import xlwt
from xlutils.copy import copy

def CB_Write():
    #The function which is used to copy from the clipboard
    Env.getClipboard()

    #create a variable to use for the clipboard item
    cb = Env.getClipboard()

    #open the workbook
    rb = xlrd.open_workbook(""C:\\SikuliX\\....\\Sample.xls")

    #copy the workbook to make it writeable
    wb = copy(rb)

    #write to the sheet and column
    #first number represents the row
    #second number represents the column
    #cb -- represent the write info from the clipboard
    sheet = wb.get_sheet(2).write(1,0,cb)

    #save the workbook
    wb.save("C:\\SikuliX\\....\\Sample.xls")

CB_Write()

----------------------------------------------------------------------------------------------
Example2 -- this example overlays the workbook

from sikuli import *
import xlrd
import xlwt
from xlutils.copy import copy

def ExcWr():
    #The function which is used to copy from the clipboard
    Env.getClipboard()

    #create a variable to use for the clipboard item and any other data needed
    cb = Env.getClipboard()
    cb1 = "Easy"
    cb2 = "Peasy2"

    #open the workbook
    rb = xlrd.open_workbook("C:\\SikuliX\\...\\SikuliWrite.xls")

    #copy the workbook to make it writeable
    wb = copy(rb)

    #specify the correct encoding of the workbook
    book = xlwt.Workbook(encoding="utf-8")

    #specify the sheetname
    sheet1 = book.add_sheet("MySheet1")

    #write the column names
    #0,0 = A1
    #0,1 = A2
    #0,2 = A3
    sheet1.write(0, 0, "Test1")
    sheet1.write(0, 1, "Test2")
    sheet1.write(0, 2, "Test3")

    #write the row data
    #1,0 = B1
    #1,1 = B2
    #1,2 = B3
    sheet1.write(1, 0, cb)
    sheet1.write(1, 1, cb1)
    sheet1.write(1, 2, cb2)

    book.save("C:\\SikuliX\\...\\SikuliWrite.xls")

ExcWr()

hope this helps those out there trying to write to Excel