【问题标题】:Copying to a specific sheet: openpyxl - destination sheet ignored when using copy_worksheet复制到特定工作表:openpyxl - 使用 copy_worksheet 时忽略目标工作表
【发布时间】:2019-06-21 16:24:15
【问题描述】:

根据这个answerthese documents,我尝试指定要写入的sourcetarget 工作表,但是当我这样做时,结果与我没有指定目标一样:

from openpyxl import load_workbook 
wb = load_workbook('MyFile.xlsx')
ws = 'Sheet1'


idx = book.index(ws)
new_ws = 'Test'
book.create_sheet(new_ws, idx+1)
source = book[ws]
target = book[new_ws]
target = book.copy_worksheet(source)
wb.save('Output.xlsx')

source = book[ws]
book.copy_worksheet(source)
wb.save('Output.xlsx')

两者都会在工作簿末尾添加一个名为 Sheet1 Copy 的新工作表。 如何将工作表复制到另一个空白工作表或工作簿中的特定位置?

【问题讨论】:

    标签: excel python-3.x openpyxl


    【解决方案1】:
    # new copied sheet was assigned to target
    target = wb.copy_worksheet(wb.source_sheet)
    
    # now just change the name to desired one
    target.title = desired_name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-21
      • 2020-06-14
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多