VBA到Python的转换

我有一个vbamacros,在excel中embedded一系列文本文件,在D列中引用A列中的值,迭代列。 在将其转换为Python的过程中,我将指定文件的embedded位置。 vbamacros将每个文件embedded到相关行中,而Python脚本以其当前forms将所有文件embedded到同一个单元格(B2)中。 我尝试了各种各样的策略,主要是在不同的地方放置胶印,没有成功,在网上找不到任何例子。 这里是代码片段:VBA:

Sub Insert_Text_File() Dim ol As OLEObject Dim file As String Dim cell As Range ' loop each cell in column A For Each cell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row) If Not IsEmpty(cell) Then file = cell.Value & ".txt" ' create and insert a new OleObject based on the path ' ThisWorkbook.path & "\" & cell & file will make the filename Set ol = Worksheets("Inventory").OLEObjects.Add(Filename:=ThisWorkbook.path & "\" & file, Link:=False, DisplayAsIcon:=True, Height:=10) ' align the OleObject with Column D - (0 rows, 3 columns to the right from column A) ol.Top = cell.Offset(0, 3).Top ol.Left = cell.Offset(0, 3).Left End If Next End Sub 

python:

 folder = 'C:\Users\ioe\\' #raw_input("Please specify Show Files directory: ") inventory_csv = 'inventory.csv' book = Workbook() sheet = book.add_sheet('Inventory',cell_overwrite_ok=True) inventory_data_to_csvfile(folder) csv_to_xls(inventory_csv) os.remove('inventory.csv') xl = win32.gencache.EnsureDispatch('Excel.Application') xl.Visible = 1 wb = xl.Workbooks.Open("C:\Users\\robertph\Share\inventory\INVENTORY.xls") column = wb.ActiveSheet.Range("A2:A200") for cell in column: if cell.Value is not None: f = 'C:\Users\\robertph\Share\ioe\\' + str(cell.Value) + '.txt' ol = wb.ActiveSheet.OLEObjects().Add(Filename=f, Link=False) #ol.Offset(0, 3) #cell.GetOffset(0, 3).Value = ol #ol_offset = ol.Cells(cell).GetOffset(0, 3) #ol.Top = cell.Offset(0, 3) #ol.Left = cell.Offset(0, 3) 

任何build议,非常感谢。 谢谢。

就像其他人发现自己处于同样的位置; 我终于解决了(唷!)

 range = wb.ActiveSheet.Range("A2:A200") for cell in range: if cell.Value is not None: f = 'C:\Users\\robertph\Share\ioe\\' + str(cell.Value) + '.txt' ol = wb.ActiveSheet.OLEObjects().Add(Filename=f, Link=False) ol.Top = cell.GetOffset(0, 3).Top ol.Left = cell.GetOffset(0, 3).Left