python如何复制excel单元格值及其颜色和公式等

python如何复制excel单元格值及其颜色和公式等,python,excel,com,win32com,openpyxl,Python,Excel,Com,Win32com,Openpyxl,搜索和尝试,到目前为止还没有找到解决方案 1.使用openpyxl e、 g.:ws_to.cell(第1行第1列).value= ws_from.cell(第2行第2列).value 2.使用win32com.client e、 g:  xlsSheet.Cells(row1, column1).Value = xlsSheet.Cells(row2, coloum2).Value 上面的两个模块都未能复制保持颜色和方程式的单元格 复制单元格时,是否有更强大的模块帮助我保持颜色和方程式?使用

搜索和尝试,到目前为止还没有找到解决方案

1.使用openpyxl

e、 g.:
ws_to.cell(第1行第1列).value= ws_from.cell(第2行第2列).value

2.使用win32com.client

e、 g: 

xlsSheet.Cells(row1, column1).Value = xlsSheet.Cells(row2, coloum2).Value
上面的两个模块都未能复制保持颜色和方程式的单元格


复制单元格时,是否有更强大的模块帮助我保持颜色和方程式?

使用
win32com.client
可以使用
Range.copy(destination)


这将复制所有内容,就像您执行ctrl-c ctrl-v(值、格式等)一样。

使用openpyxl,您可以执行以下操作:

# With openpyxl 1.8:
#  Make sure you're loading with load_workbook('file.xlsx', data_only=False)
#  Note: this behavior may be changing with openpyxl 1.9 in the future

# First, copy the value
ws_to.cell(row1, column1).value = ws_from.cell(row2,column2).value

# Second, copy the style
# Styles are stored in a table on the worksheet, you can directly copy there
# Also, set the static flag, so it knows the style is shared
# And will make a copy if you edit the style on the cell afterwards
from_style = ws_from.cell(row2,column2).style
to_coord = ws_to_cell(row1, column1).get_coordinate()
from_style.static = True  # Means the style is shared, usually only set when reading a file
ws._styles[to_coord] = from_style

使用openpyxl 2.3.5,这将成功地将一个单元格复制到另一个单元格,包括颜色/方程式:

to_cell = ws_to.cell(row1, column1)
from_cell = ws_from.cell(row2,column2)

to_cell.value = from_cell.value
to_cell.style = from_cell.style

.Value
将只接受该值,不带任何格式。查找其他属性(或者查看是否可以在不使用
.value
的情况下使用这些库)并查看复制的内容。查看是否没有合适的答案?使用
openpyxl
有什么方法可以做到这一点吗?@Jean-FrancoisT。关于如何使用OpenPyxl实现这一点,已经有了答案。真奇怪,我没有看到它。谢谢。是
to_coord=ws_to_cell
还是
to_coord=ws_to.cell
(带
)?
to_cell = ws_to.cell(row1, column1)
from_cell = ws_from.cell(row2,column2)

to_cell.value = from_cell.value
to_cell.style = from_cell.style