def ex_temp():
wb = openpyxl.load_workbook("input.xlsm",data_only=False, keep_vba=True)
wb2 = openpyxl.load_workbook("input.xlsm",data_only=True, keep_vba=True)
sheet = wb.get_sheet_by_name('Sheet1')
sheet2=wb2.get_sheet_by_name('Sheet1')
print "With Formula: ",sheet.cell("C2").value, sheet.cell("D2").value
print "With Only Data: ",sheet2.cell("C2").value, sheet2.cell("D2").value
sheet.cell("C2").value=100
wb.save("output.xlsm")
# wb2.save("output2.xlsm")
print "After Writing-------------------------------------------------------"
wb = openpyxl.load_workbook("output.xlsm",data_only=False, keep_vba=True)
wb2 = openpyxl.load_workbook("output.xlsm",data_only=True, keep_vba=True)
print wb.get_sheet_names()
sheet = wb.get_sheet_by_name('Sheet1')
sheet2 = wb2.get_sheet_by_name('Sheet1')
print "With Formula: ",sheet.cell("C2").value,sheet.cell("D2").value
print "With Only Data: ",sheet2.cell("C2").value, sheet2.cell("D2").value
ex_temp()
With Formula: 5 =C2
With Only Data: 5 5
After Writing-------------------------------------------------------
[u'Sheet1']
With Formula: 100 =C2
With Only Data: 100 None
Solution:
Reopen it once and save it.
from win32com.client import Dispatch
def just_open(filename):
xlApp = Dispatch(“Excel.Application”)
xlApp.Visible = False
xlBook = xlApp.Workbooks.Open(filename)
xlBook.Save()
xlBook.Close()
Hope this solution works for you!!