我想将一个上下文菜单项添加到我的Windows资源管理器(Windows 7),以便将所选文件导入Excel并启动导入对话框。
我设法添加了本指南后面的条目: http://www.howtogeek.com/107965/how-to-add-any-application-shortcut-to-windows-explorers-context-menu/
但是当它打开文件时,会跳过导入对话框并在电子表格中打开文件。
编辑:我搜索了exe开关,但是我找到的那些对此没用! EDIT2:ElectricLlama提出的更多背景额外背景: 将需要导入大量文本文件到excel。不是CSV 目前它们是固定宽度的,但它们肯定会在不久的将来发生变化。 我打开它们来转换它们并添加一些额外的信息作为列。 程序X生成一个带有空格的表格,用于间距----> Excel格式化此表并添加信息---->程序Y直接打开excel文件
答案 0 :(得分:1)
所以我在windows注册表中做了一个选项(使用regedit)让它启动python脚本,该脚本将处理解析并调用Excel.exe。文件类型是没有扩展名的文件,python是在PATH windows变量中。还有regedit导出转义引号(如果你想知道的话):
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\import with excel]
@="Convert and Open with Excel"
[HKEY_CLASSES_ROOT\*\shell\import with excel\command]
@="python \"path to python script\" \"%1\""
python脚本非常混乱,非常适合我的需求,但这里是:
import csv, os, sys
#If there are no arguments stop everything
if len(sys.argv) > 0:
file = sys.argv[1]
else: sys.exit()
fileout = file + ".csv"
data = open(file, "r")
open(fileout, 'w').close() #cleans the file
dataout = open(fileout, "a")
# using the python csv lib, please look for info on it for further explanation
with open(fileout, 'wb') as csvfile:
spamwriter = csv.writer(csvfile, dialect='excel')
# I needed a special handling for the first line, that's why I used the i < 2
i = 1
for line in data:
line = line.split()
lineout = [] # clean the list after each line is parsed
if i < 2: lineout.append("")
for item in line:
if item != "\"": lineout.append(item.replace("\"", ""))
spamwriter.writerow(lineout)
i += 1
data.close()
dataout.close()
# launches excel.exe with the fileout argument
os.system("start Excel.exe \""+ fileout + "\"")
然后我按照自己的意愿获得在excel中打开的csv文件。这种方式实际上更快,然后必须手动选择Excel导入窗口中的每个选项。