简体   繁体   English

保存 excel 表/进行更改时出错 — Python openpyxl

[英]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.我正在尝试使用 openpyxl 写入在创建文件/目录名称时解析的一些数据。 I can't posst my full ncode because it has work directories and such in it but here's the failing portion:我无法发布完整的 ncode,因为它有工作目录等,但这是失败的部分:

############################
#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)如果我取消注释保存,我会收到附加的 excel 错误(见图)

Going home for the day, but any help appreciated.回家一天,但任何帮助表示赞赏。 Will check in Monday!将在星期一检查!

Excel Error Excel 错误

Using @Greg 's comment I was able to find another answer that provided the solution.使用@Greg 的评论,我能够找到另一个提供解决方案的答案。 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)
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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