0

我有一个带有名为 PRODRLIST 的按钮的用户窗体,当您单击它时,它应该对 My Product.csv 进行排序并将其输出保存为 My Product.grouped.csv。我可以在 IDLE 中运行 Python 代码并且它可以工作,但是当我定义一个函数 (Sort_File()) 时,我无法让它工作。我对 Python 还是很陌生,所以我假设它很简单。

用于启动 Python 程序的 Excel VBA 代码:

Private Sub PRODRLIST_Click()
RunPython ("import SortProduct; SortProduct.Sort_File()")
End Sub

对 .csv 文件进行排序的 Python 代码:

from xlwings import Workbook, Range
# file group_by_trailing_py2.py
import os
import csv
from itertools import groupby

DELIM=';'
IN_FILENAME = 'My Product.csv'
OUT_FILENAME = 'My Product.grouped.csv'


keyfunc = lambda row: row[1:]

with open(IN_FILENAME) as csv_file:
    rows = sorted(csv.reader(csv_file, delimiter=DELIM), key=keyfunc)

it = map(lambda t: [", ".join(v[0].strip() for v in t[1]) + " "] + t[0],
        groupby(rows, key=keyfunc))

with open(OUT_FILENAME, 'w') as csv_file:
    writer = csv.writer(csv_file, delimiter=DELIM)  
for row in it:
    writer.writerow(row)  

也许我必须定义更多的东西或者这是我的缩进?

4

1 回答 1

0
  Private Sub PRODRLIST_Click()
    Dim pyPrgm As String, pyScript As String
    pyPrgm = "C:\Python33\python.exe " 
'---- Space should be there after python 'exe path
    pyScript = "C:\Python33\MyScripts\sort.py" ' File path
    Call Shell(pyPrgm & pyScript, vbMaximizedFocus)
    End Sub
于 2015-11-11T12:06:34.060 回答