copy & paste isolation with win32com & python

前端 未结 3 684
失恋的感觉
失恋的感觉 2021-01-14 17:08

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?

<
3条回答
  •  孤城傲影
    2021-01-14 17:57

    Just read the data out into python, and then write it back.

    Instead of:

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

    Process the data cell-by-cell:

    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 ..
    

    I'm not sure what pasting a two-dimensional selection into a single cell does, so I can't do the last line for you. You may find that you need to do two loops; one to read in the data, and then a second to write it back out.

提交回复
热议问题