简体   繁体   English

使用 xlsxwriter,无法打开 Excel 文件。 文件扩展名无效

[英]Using xlsxwriter, Excel file cannot be opened. File extension is not valid

I am trying to insert a macro when creating an Excel file using xlsxwriter library.我试图在使用 xlsxwriter 库创建 Excel 文件时插入一个宏。 I followed the tutorial example from the documentation: Working with VBA Macros .我遵循文档中的教程示例:使用 VBA 宏

The file was created but when attempting to open with Microsoft Excel, it prompted the following message :该文件已创建,但在尝试使用 Microsoft Excel 打开时,提示以下消息:

Excel cannot open the file because the file format or file extension is not valid. Excel 无法打开该文件,因为文件格式或文件扩展名无效。 Verify that the file has not been corrupted and that the file extension matches the format of the file.验证文件未损坏并且文件扩展名与文件格式匹配。

Note:笔记:

  1. I am using Microsoft Office Excel 2007 (12.0.6425.1000) SP2 MSO version on Windows 10.我在 Windows 10 上使用 Microsoft Office Excel 2007 (12.0.6425.1000) SP2 MSO 版本。
  2. Opening with LibreOffice is fine but click the button is not responding anything (since it cannot read VBA).使用 LibreOffice 打开很好,但单击按钮没有任何响应(因为它无法读取 VBA)。
def action_macro_test(self):
        workbook = xlsxwriter.Workbook("/home/user1/Desktop/macro_test.xlsm")
        worksheet = workbook.add_worksheet("Sheet1")

        worksheet.set_column('A:A', 30)

        # Add the VBA project binary.
        workbook.add_vba_project('./vbaProject.bin')

        # Show text for the end user.
        worksheet.write('A3', 'Press the button to say hello.')

        # Add a button tied to a macro in the VBA project.
        worksheet.insert_button('B3', {'macro':   'say_hello',
                                    'caption': 'Say Hello!',
                                    'width':   80,
                                    'height':  30})

        workbook.close()

You created a workbook with macros, but saved it as .xlsx .您使用宏创建了一个工作簿,但将其保存为.xlsx Excel does not allow macros in .xlsx files. Excel 不允许在.xlsx文件中使用宏。 They must be .xlsm .它们必须是.xlsm

I ran your code, with only the path modified, and it created a file that opened as expected:我运行了你的代码,只修改了路径,它创建了一个按预期打开的文件:

import xlsxwriter

def action_macro_test(self):
        workbook = xlsxwriter.Workbook("macro_test.xlsm")
        worksheet = workbook.add_worksheet("Sheet1")

        worksheet.set_column('A:A', 30)

        # Add the VBA project binary.
        workbook.add_vba_project('./vbaProject.bin')

        # Show text for the end user.
        worksheet.write('A3', 'Press the button to say hello.')

        # Add a button tied to a macro in the VBA project.
        worksheet.insert_button('B3', {'macro':   'say_hello',
                                    'caption': 'Say Hello!',
                                    'width':   80,
                                    'height':  30})

        workbook.close()

action_macro_test(None)

Output :输出

在此处输入图片说明

I used the vbaProject.bin file that is in the examples directory of the XlsxWriter distro and I tested it with several different versions of Excel.我使用了位于 XlsxWriter 发行版示例目录中的vbaProject.bin文件,并使用多个不同版本的 Excel 对其进行了测试。

Can, you try the macros.py example in the examples directory of the distro and see if that works for you.可以,您可以尝试发行版示例目录中的macros.py示例,看看它是否适合您。 After that ensure you have a valid vbaProject.bin file in your example.之后确保您的示例中有一个有效的vbaProject.bin文件。

It turns out that the file vbaProject.bin path is not correctly set.原来是文件 vbaProject.bin 路径设置不正确。 It is working fine now.它现在工作正常。 Sorry for the false alarm.抱歉误报。 thanks for the help.谢谢您的帮助。

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

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