0
我的工作簿有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/ –