将Range(
['W7:W46'])从一个工作表复制到另一个工作表:
如果范围 不 重叠,则也可以在同一工作表中。
from openpyxl import Workbook# Create a new Workbookwb = Workbook()ws = wb.worksheets[0]from openpyxl.utils import range_boundaries# Define start Range(['J2']) in the new Worksheetmin_col, min_row, max_col, max_row = range_boundaries('J2')# Iterate Range you want to copyfor row, row_cells in enumerate(New_Burden['W7:W46'], min_row): for column, cell in enumerate(row_cells, min_col): # Copy Value from Copy.Cell to given Worksheet.Cell ws.cell(row=row, column=column).value = cell.value
如果您要对多个不同的列执行上述 *** 作,请在中使用以上内容
function:
def copy_range(source_range, target_start): # Define start Range(target_start) in the new Worksheet min_col, min_row, max_col, max_row = range_boundaries(target_start) # Iterate Range you want to copy for row, row_cells in enumerate(New_Burden[source_range], min_row): for column, cell in enumerate(row_cells, min_col): # Copy Value from Copy.Cell to given Worksheet.Cell ws.cell(row=row, column=column).value = cell.valuefor source_range, target_start in [('W7:W46','J2'), ('Y7:Y46','A2')]: copy_range(source_range, target_start)
使用Python测试:3.4.2-openpyxl:2.4.1-LibreOffice:4.3.3.2
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)