使用 win32com 和 python 复制和粘贴隔离

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/530682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:19:16  来源:igfitidea点击:

copy & paste isolation with win32com & python

pythonexcel

提问by Thorvaldur

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?

有没有办法使用 python 和 win32com 来复制和粘贴,这样 python 脚本可以在后台运行而不会弄乱“用户”的复制和粘贴能力?

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.

好的,让问题更清楚一些,我需要格式化,所有这些,所以只是获取 t 值当然很简单,但并不完全是需要的。

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?

那么让我将问题表述为:为什么在没有选择、复制和粘贴例程的情况下在 python 中获取值及其 excel 格式?

回答by ta2-1

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

根据MSDN:Excel 对象模型参考

回答by John Fouhy

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

只需将数据读入python,然后将其写回即可。

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.

我不确定将二维选择粘贴到单个单元格中有什么作用,所以我不能为你做最后一行。你可能会发现你需要做两个循环;一个读入数据,然后第二个写回。

回答by sliders_alpha

A little late, but I think I have your solution, and since I had trouble finding it it'll help someone anyway. This piece of code will copy everything (value, formula, formating, etc) without using selections (thus not messing with your users, and this is faster than using selections)

有点晚了,但我想我有你的解决方案,因为我很难找到它,无论如何它都会帮助别人。这段代码将在不使用选择的情况下复制所有内容(值、公式、格式等)(因此不会干扰您的用户,这比使用选择更快)

First I open excel like that :

首先我像这样打开excel:

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

To copy and paste on the same worksheet while conserving formating, formulas, etc:

要在保存格式、公式等的同时复制并粘贴到同一工作表上:

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

To copy and paste on an other worksheet while conserving formating, formulas, etc:

要在保留格式、公式等的同时复制并粘贴到其他工作表上:

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

case sensitive.

区分大小写。