我有一个带有名为PRODRLIST的按钮的UserForm,当你点击它时,应该对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)
也许我必须定义更多的东西,或者这是我的缩进?
答案 0 :(得分: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