Copy and paste isolation with win32com and python

Is there a way to use python and win32com to copy and paste, so that python scripts can run in the background, and not spoil the ability to "copy and paste" users?

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

suppose the script runs continuously on a large collection of datasets ...

Well, another clarity on the issue, I need a formation, all this, so just grabbing the values โ€‹โ€‹of t is, of course, simple, but not quite what you need.

So let me tell you the following: What is there, why do I need to capture both the value and its forcing in python without the procedure of selecting, copying and pasting?

+3
source share
4 answers

Instead:

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"))

MSDN: Excel

+5

python, .

:

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

:

for r in range(1, row+1):    # I think Excel COM indexes from 1
    for c in range (1, 12):  # a--k
        val = ws.Cells(r, c).Value
        ws.Cells(r, c).Value = ''    # Blank this cell; equivalent to cut
        ws.Cells(?, ?).Value = val   # Write it somewhere ..

, , . , ; , , .

+1

, , , , -. (, , ..) ( , , , )

excel :

xl = client.Dispatch("Excel.Application")
wb = xl.Workbooks.Open("c:/somepath/file.xls")
xl.Visible = 1

, , ..:

ws = wb.Sheets("someSheet") #you can put the sheet number instead of it name
ws.Range('a1:k%s' % row).Copy() #copy works on all range objects (ws.Columns(), ws.Cells, etc)
ws.Paste(ws.Range('a7'))

, , ..:

source_ws = wb.Sheets("SheetContainingData")
destination_ws = wb.Sheets("SheetWhereItNeedsToGo")
source_ws.Range('a1:k%s' % row).Copy()
destination_ws.Paste(destination_ws.Range('a7'))

.

+1

How to convert to dynamic range? I mean the above people used Range ("A1: D4"). How to convert a range to no. notation. Like start row: 1, start column: 1 to end row: 4, end column: 4? Any help would be appreciated. I want to change this code from static to dynamic range: ws.Range ("A1: K5"). Copy (ws.Range ("A7: K11"))

0
source

Source: https://habr.com/ru/post/1702947/


All Articles