擅长:python、mysql、java
<p>使用openpyxl,您可以这样做:</p>
<pre><code># 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
</code></pre>