简体   繁体   中英

Why is the file getting corrupted?

This is my python script, to write data to an excel file and also add a macro. The macro will just launch adobe reader

import xlsxwriter
from win32com.client import Dispatch
import os

Filename = "Failure_Analysis_Report_2.xlsm"
directory = "C:\\Users\\Avik\\Desktop\\"

workbook = xlsxwriter.Workbook( directory + Filename )
worksheet = workbook.add_worksheet()

header = [] #my header row
format_header = workbook.add_format({'bold': True , 'size' : 16 , 'bg_color' : '#F8CBAD' , 'border' : 5})

for i in range(5):
    worksheet.write ( 0 , i , header[i] , format_header)

worksheet.set_column(0 , 0 , 15 )
worksheet.set_column(1 , 1 , 60 )
worksheet.set_column(2 , 2 , 35 )
worksheet.set_column(3 , 3 , 50 )
worksheet.set_column(4 , 4 , 15 )

xl_inp = [[,,,]] #my list of lists to input

wrap = workbook.add_format()
wrap.set_text_wrap()

a = 1

for x in xl_inp :
    b = 0
    for y in x :
        worksheet.write ( a , b , y , wrap)
        b += 1
    a += 1

worksheet.insert_button ( 'C7' , {'macro' : 'Auto_Open', 'caption' : 'Acrobat' , 'width' : 135} )

workbook.close()

com_instance = Dispatch("Excel.Application")
com_instance.Visible = False
objworkbook = com_instance.Workbooks.Open( directory + Filename )
xlmodule = objworkbook.VBProject.VBComponents.Add(1)
sCode = '''Sub Auto_Open()
    Dim x As Variant
    Dim Path As String

    Path = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"

    x = Shell(Path, vbNormalFocus)
    End Sub'''
xlmodule.CodeModule.AddFromString(sCode)
objworkbook.SaveAs( os.path.join ( directory , Filename ) )
del com_instance

while running this script, I'm getting the error I shared below. The file is getting corrupted and i don't know why, any help will be much appreciated.

Also, if I remove the macro and just save it as *.xlsx, its works fine. Even if I don't remove the macro and still save it as *.xlsx it still works fine, although it prompts me to save it without VB Macros which is not the issue.

My issue being that it couldn't save the data in *xlsm format (with or without data)

any help is appreciated. Thanks.

Traceback (most recent call last):
  File "C:\Users\Avik\Desktop\trying_xl_2.py", line 43, in <module>
objworkbook = com_instance.Workbooks.Open( directory + Filename )
  File "C:\Users\Avik\AppData\Local\Temp\gen_py\3.6\00020813-0000-0000-C000-000000000046x0x1x9\Workbooks.py", line 78, in Open
, Converter, AddToMru, Local, CorruptLoad)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Excel cannot open the file 'Failure_Analysis_Report_2.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.", 'xlmain11.chm', 0, -2146827284), None)

Its done.

Like @jmcnamara suggested, I saved the original content in a .xlsx file

Filename = "Failure_Analysis_Report_2.xlsx"

took another variable:

Filename_2 = "Failure_Analysis_Report_2.xlsm" 

opened the workbook using Dispatch added the VBA code and Module accordingly, and the saved the workbook using this syntax :

objworkbook.SaveAs( os.path.join ( directory , Filename_2 ) , 52 )

That 52 parameter helps to save it in the .xlsm format previously, I wasn't providing any parameter, so it was trying to save in .xlsx itself and was thus having issues saving it with the macros.

Also I had to add :

objworkbook.Close()
com_instance.Application.Quit()

after

objworkbook.SaveAs( os.path.join ( directory , Filename_2 ) , 52 )

before

del com_instance

Without these 2 syntax the 'Excel.Application' was running in the background and had to closed manually.

For further details regarding the parameter values follow this :

https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat

Hope this resolution helps someone in the future. Cheers!

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