Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy & paste isolation with win32com & python

Tags:

python

excel

Is there a way of using python and win32com to copy and paste such that python scripts can run in the background and not mess up the "users" copy and paste ability?

from win32com.client import Dispatch
import win32com.client

xlApp = Dispatch("Excel.Application")
xlWb = xlApp.Workbooks.Open(filename_xls)
ws = xlWb.Worksheets(1)
xlApp.Visible=False

ws.Range('a1:k%s' % row).select
ws.Range('a1:k%s' % row).cut
ws.Range('a7').select
ws.paste

assume that the script will be running continuously on a large collection of datasets...

Ok, some more clarity to the question, I need the formating, all of it, so just grabbing t values is of course simple, but not exactly what is needed.

So then let me phrase the question as: What is there a why to grab both the value and its excel formating in python without the select, copy, and paste routine?

like image 470
Thorvaldur Avatar asked Mar 02 '23 00:03

Thorvaldur


1 Answers

Instead of:

ws.Range('a1:k%s' % row).select
ws.Range('a1:k%s' % row).cut
ws.Range('a7').select
ws.paste

I did:

ws.Range("A1:K5").Copy(ws.Range("A7:K11"))

according to MSDN: Excel Object Model Reference

like image 116
ta2-1 Avatar answered Mar 03 '23 20:03

ta2-1