我正在研究一个VBA代码,该代码调用保存在桌面上的python脚本。这是我的VBA代码。
Shell "cmd /k C:\Python27\python.exe C:\Users\ashish.baboo\Desktop\UKT-SL-Ashish\Cleaning-Tools\Old-Geocode.py"
waitTill = Now() + TimeValue("00:00:40")
While Now() < waitTill
DoEvents
此Python脚本在放置在桌面的Excel文件中加载地址。当我直接运行python脚本时,它执行完美。但是当我运行VBA代码时,Python脚本会打开但是会给我一个excel文件不可用的错误。这是我的Python脚本代码:
import xlrd
import geocoder
import openpyxl
from openpyxl import load_workbook
location = "C:\\Users\\ashish.baboo\\Desktop\\UKT-SL-Ashish\\Cleaning-Tools\\1.xlsx"
workbook = xlrd.open_workbook(location)
sheet = workbook.sheet_by_index(1)
Rows = sheet.nrows
Cols = sheet.ncols
data = [[sheet.cell_value(r,c) for c in range(Cols)] for r in range(Rows)]
wb = load_workbook('1.xlsx')
ws1 = wb.get_sheet_by_name("Locations")
for i in range(1,Rows):
add = ws1.cell(row = i+1, column = 19).value
cadd = geocoder.google(add , language = "en" , key = "AIzaSyBsiH6LubuA9dn1s97UsUcesQRw71isBuo")
print(i , cadd.latlng , cadd.quality ,cadd.country)
S_no = ws1.cell(row = i+1, column = 1)
O_Housenumber = ws1.cell(row = i+1, column = 3)
O_Street = ws1.cell(row = i+1, column = 4)
O_City = ws1.cell(row = i+1, column = 5)
O_County = ws1.cell(row = i+1, column = 6)
O_State = ws1.cell(row = i+1, column = 7)
O_Postal = ws1.cell(row = i+1, column = 8)
O_Country = ws1.cell(row = i+1, column = 9)
Latitude = ws1.cell(row = i+1, column = 10)
Longitude = ws1.cell(row = i+1, column = 11)
Accuracy = ws1.cell(row = i+1, column = 12)
S_no.value = i
O_Housenumber.value = cadd.housenumber
O_Street.value = cadd.street
O_City.value = cadd.city
O_County.value = cadd.county
O_State.value = cadd.state_long
O_Postal.value = cadd.postal
O_Country.value = cadd.country_long
Latitude.value = cadd.lat
Longitude.value = cadd.lng
Accuracy.value = cadd.quality
wb.save('1.xlsx')
我已经尝试了很多解决方案,但没有成功。请建议如何解决它?
答案 0 :(得分:1)
这一行:
wb = load_workbook('1.xlsx')
依赖程序知道在哪里查找文件。如果你从VBA启动Python,那么工作目录几乎肯定会有所不同。所以你的Python程序需要知道完整的文件路径。您可以在程序中对其进行硬编码,也可以从命令行将其传递给程序。
答案 1 :(得分:0)
使用绝对路径而不是相对路径可以解决问题!