Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88868

Why is the file getting corrupted?

$
0
0

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)

Viewing all articles
Browse latest Browse all 88868


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>