我在python中使用win32com进行excel控制并面临一个小问题。

我想将Excel单元格(1,1)(即B2)复制到“ A1”

python - Win32的Excel复制除了功能-LMLPHP

下面是我的代码

Sheet.Range(Sheet.Cells(1,1), Sheet.Cells(LastRow, Lastcol)).copy(Sheet1.Range("A1"))


可以,但是可以复制公式而不是公式值(结果)。
(我希望“ 0”不是“ = SUM(C1,C2)”)

请指导,如何更改代码以获得所需的结果?

最佳答案

由于目标单元格的格式不正确,可能会发生这种情况。因此,您的代码需要更改为以下内容。我建议将您现有的行替换为以下内容:

'Change the format of cell to General as Text fields do not evaluate the formula
Sheet.Range(Sheet.Cells(1,1), Sheet.Cells(LastRow, Lastcol)).NumberFormat = "General"

'Copy the formula and assign it as formula (rather than copying it)
Sheet.Range(Sheet.Cells(1,1), Sheet.Cells(LastRow, Lastcol)).Formula  = Sheet1.Range("A1").Formula


希望能帮助到你 !

关于python - Win32的Excel复制除了功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53643435/

10-09 01:59
查看更多