【问题标题】:Reading and updating sheets in an XLSM file using pandas while preserving the VBA code使用 pandas 读取和更新 XLSM 文件中的工作表,同时保留 VBA 代码
【发布时间】:2019-08-03 15:59:26
【问题描述】:

我需要读取一个 xlsm 文件并更新文件中的一些工作表。我想为此目的使用 pandas。

我尝试了以下帖子中提供的答案。当我重新添加 VBA 项目时,我看不到 VBA 宏。
https://stackoverflow.com/posts/28170939/revisions

这是我尝试过的步骤,

从original.xlsm文件中提取VBA_project.bin然后

writer = pd.ExcelWriter('original.xlsx', engine='xlsxwriter')
workbook = writer.book
workbook.filename = 'test.xlsm'
workbook.add_vba_project('vbaProject.bin')
writer.save()

我没有看到附加到“test.xlsm”的 VBA 宏。即使我将它写入“original.xlsm”文件,结果也是一样的。

如何保留 VBA 宏或将它们添加回原始 xlsm 文件?

另外,有没有办法我可以使用pd.ExcelWriter 打开“xlsm”文件本身而不是“xlsx”对应文件?

【问题讨论】:

  • 我认为您应该为此使用 VBA,而不是 Python。只是我的 .02。
  • 文件很大,目的是将原始文件的一个特定工作表合并到最近的更新中,例如将本月的数据集添加到每年累积的数据表中。

标签: python excel vba pandas


【解决方案1】:

你可以用 pandas 轻松做到这一点

import pandas as pd
import xlrd

# YOU MUST PUT sheet_name=None TO READ ALL CSV FILES IN YOUR XLSM FILE
df = pd.read_excel('YourFile.xlsm', sheet_name=None)

# prints all sheets
print(df)

【讨论】:

    【解决方案2】:

    啊,我明白了。我仍然不知道你在做什么,但这里有一些让 Python 与 Excel 通信的通用代码示例。

    Read contents of a worksheet in Excel:
    
    import pandas as pd
    from pandas import ExcelWriter
    from pandas import ExcelFile
    
    df = pd.read_excel('C:\\your_path\\test.xls', sheetname='Sheet1')
    
    ************************************************************************************
    
    Use Python to run Macros in Excel:
    import os
    import win32com.client
    
    #Launch Excel and Open Wrkbook
    xl=win32com.client.Dispatch("Excel.Application")  
    xl.Workbooks.Open(Filename="C:\your_path\excelsheet.xlsm") #opens workbook in readonly mode. 
    
    #Run Macro
    xl.Application.Run("excelsheet.xlsm!modulename.macroname") 
    
    #Save Document and Quit.
    xl.Application.Save()
    xl.Application.Quit() 
    
    #Cleanup the com reference. 
    del xl
    

    Write, from Python, to Excel:
    
    import xlsxwriter
    
    # Create an new Excel file and add a worksheet.
    workbook = xlsxwriter.Workbook('C:/your_path/ranges_and_offsets.xlsx')
    worksheet = workbook.add_worksheet()
    
    # Widen the first column to make the text clearer.
    worksheet.set_column('A:A', 20)
    
    # Add a bold format to use to highlight cells.
    bold = workbook.add_format({'bold': True})
    
    # Write some simple text.
    worksheet.write('A1', 'Hello')
    
    # Text with formatting.
    worksheet.write('A2', 'World', bold)
    
    # Write some numbers, with row/column notation.
    worksheet.write(2, 0, 123)
    worksheet.write(3, 0, 123.456)
    
    
    workbook.close()
    

    from openpyxl import Workbook
    wb = Workbook()
    
    # grab the active worksheet
    ws = wb.active
    
    # Data can be assigned directly to cells
    ws['A1'] = 42
    
    # Rows can also be appended
    ws.append([1, 2, 3])
    
    # Python types will automatically be converted
    import datetime
    ws['A2'] = datetime.datetime.now()
    
    # Save the file
    wb.save("C:\\your_path\\sample.xlsx")
    

    【讨论】:

    • 感谢您的示例。我的用例与您提供的第二个示例一致。 #workbook1 有 10 个工作表 workbook1 = xlsxwriter.Workbook('C:/your_path/test1.xlsm') #workbook2 有 1 个工作表需要与相应工作表合并
    • 在第一个文件 workbook2 = xlsxwriter.Workbook('C:/your_path/test2.xlsm') 我实际上可以通过拉动 vba_project.bin 并将这两个文件都视为“xlsx”并进行合并。但是当将 vba_project.bin 添加回合并的 xlsx 文件并将其重命名为“xlsm”时,我看不到宏。未应用 VBA 内容
    • pandas.read_excel() 0.21 版中,指定工作表名称的选项现在是sheet_name,而不是sheetname
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 2020-07-02
    相关资源
    最近更新 更多