简体   繁体   中英

Touble with saving excel sheet/ making changes — Python openpyxl

I am trying to automate the process of creating new cost sheets at my work. I have a functional code that pulls the source file, renames it, moves it where I want it, etc.

I am trying to use openpyxl to write in some of the data that is parsed in creating the file/directory name. I can't posst my full ncode because it has work directories and such in it but here's the failing portion:

############################
#Write Info Into Cost Sheet#
############################

myworkbook=openpyxl.load_workbook(new_CSdst_file_name)
worksheet= myworkbook['COST SHEET']
worksheet['C3']= now.strftime("%m/%d/%Y")
worksheet['C5']='SOW-'+line_split[2]
worksheet['C6']='CS-'+line_split[4]
worksheet['C10']='0'
worksheet['J4']=line_split[3]
#myworkbook.save(new_CSdst_file_name)

If the save is commented out the file is created, but no cells are filled. If I uncomment the save I get the attached excel error (see image)

Going home for the day, but any help appreciated. Will check in Monday!

Excel Error

Using @Greg 's comment I was able to find another answer that provided the solution. Working code here:

    myworkbook=openpyxl.load_workbook(new_CSdst_file_name, keep_vba=True)
    worksheet= myworkbook['COST SHEET'] #Open Sheet Named COST SHEET (current Sheet 1 name in xlsm)
    worksheet['C3']= now.strftime("%d-%b-%Y") #Date
    worksheet['C5']='SOW-'+ PartNum #SOW number
    worksheet['C6']='CS-'+ PartNum #CS number
    worksheet['C7']= Description #descriptor
    worksheet['C10']='0' #creating revision 0
    worksheet['J4']= Customer #Company Name
    myworkbook.save(new_CSdst_file_name)
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM