当使用xlrd从excel获取变量时,尝试在python中使用'\'和'os.startfile'

问题描述 投票:0回答:1

我正在尝试使用python打开jpg文件时遇到问题,当我从excel中获取名称时。

这是我到目前为止在代码中所拥有的:

print(worksheet.cell(rowidx,2))
image = worksheet.cell(rowidx,11)
print(image)
image_name = str(image).replace("'","")
print(image_name)
image_name2 = image_name.replace("text:","")
print(image_name2)
os.startfile(image_name2)

我使用print函数来帮助我调试。

每当输入进入时,对于这个例子,让我们说"d:\cow.jpg",显示以下输出:

text:'d:\\cow.jpg'
text:d:\\cow.jpg
d:\\cow.jpg
Traceback (most recent call last)
  File "<pyshell#2>", line 1, in <module>
    generate(1, all, all, all, all, 1, 1, "Murloc")
  File "C:\Users\Alexander\Dropbox\Documents\Python\Random Hearthstone Card
Gen\Random Hearthstone Card Generator.py", line 38, in generate
    os.startfile(image_name2)
FileNotFoundError: [WinError 15] The system cannot find the drive specified:
'd:\\\\cow.jpg'
python excel python-3.x
1个回答
0
投票

我解决了!我只是将单元格类型从常规更改为文本,并将\替换为\\。

© www.soinside.com 2019 - 2024. All rights reserved.