Openpyxl保留公式但复制值
问题描述:
我的工作簿有6张。最后3张是我目前手动将值复制并粘贴到前3张中的一组公式。我正在使用wb.copy_worksheet()生成副本并将工作簿加载为data_only = True。但是,当我保存时,公式都会因data_only加载而丢失。有没有办法我可以复制这些值,但保持公式?这些纸张太大而不能逐个移动。Openpyxl保留公式但复制值
继承人我的代码:
import openpyxl
wb = openpyxl.load_workbook("symbols.xlsx", data_only=True)
ws = wb.get_sheet_names()
print (ws)
Value = ws[0:3] #set equal to first 3 sheets
BB = ws[3:7] #set equal to last 3 sheets
for s in range(0, len(Value)):
CopyBB = wb.copy_worksheet(wb[BB[s]]) #copy from bb sheet
CopyBB.title = Value[s]
myorder = [6, 7, 8, 3, 4, 5, 0, 1, 2] #this is to reorder the sheets that got copied.
wb._sheets =[wb._sheets[i] for i in myorder]
wb.remove_sheet(wb.worksheets[8])
wb.remove_sheet(wb.worksheets[7])
wb.remove_sheet(wb.worksheets[6])
wb.worksheets[0].title = "Securities Values"
wb.worksheets[1].title = "Indices Values"
wb.worksheets[2].title = "Currencies Values"
return wb.save("symbols.xlsx")
你能告诉我们所有的代码,包括加载部分? –
修改原稿以显示完整的代码。 –
请参阅https://stackoverflow.com/questions/40000170/ –