【发布时间】:2018-12-07 08:24:38
【问题描述】:
在您将此标记为转发之前,请阅读我的问题 xD 我无法找到我的特定问题的任何答案。
我正在尝试从 C# 程序启动 Python 脚本。现在我已经制作并测试了我的 Python 脚本,当从我的 IDE (Visual Studio 2017) 运行它时,它可以完美运行。现在,当我尝试从我的 C# 程序将其作为进程执行时,会短暂出现一个命令提示符(当我简短地说它更像是在屏幕上闪烁时),但我的脚本没有运行。
我的 python 脚本的代码:
import os
import shutil
from pptx import Presentation
from pptx.table import Table
from pptx.text.text import TextFrame
from pptx.text.text import Pt
from pptx.dml.color import RGBColor
#Get ressources dir:
currentWorkingDir = os.getcwd()
ressourceDir = currentWorkingDir[:-31 ]
#Open files and read into variables
agendaInputFile = open(ressourceDir + "agendaInputs.txt", "r", encoding="utf8")
agendaInputs = agendaInputFile.readlines()
agendaTimesFile = open(ressourceDir + "agendaTimes.txt", "r", encoding="utf8")
agendaTimes = agendaTimesFile.readlines()
meetingTitleFile = open(ressourceDir + "meetingTitle.txt", "r", encoding="utf8")
meetingTitle = meetingTitleFile.readline()
presentersFile = open(ressourceDir + "presenters.txt", "r", encoding = "utf8")
presenters = presentersFile.readlines();
#Opens the template ppt-file
prs = Presentation(ressourceDir + "agendaTemplate.pptx")
#selects the first slide
slide = prs.slides[0]
#Sets the meetingtitle
title = slide.shapes[1]
title.text = meetingTitle[:-1]
#finds the table
graphicFrame = slide.shapes[2]
table = graphicFrame.table
#sætter agendapunkterne
i = 0
while i < len(agendaInputs):
cell = table.cell(i+1,1)
textFrame = cell.text_frame
run = textFrame.paragraphs[0].add_run()
font = run.font
font.name = 'TSTAR PRO'
font.size = Pt(16)
run.text = agendaInputs[i][:-1]
font.color.theme_color = 5
i += 1
#Sætter agendatiderne
i = 0
while i < len(agendaTimes):
cell = table.cell(i+1,0)
textFrame = cell.text_frame
run = textFrame.paragraphs[0].add_run()
font = run.font
font.name = 'TSTAR PRO'
font.size = Pt(16)
run.text = agendaTimes[i][:-1]
font.color.theme_color = 5
i += 1
#Sætter presenter
i = 0
while i < len(presenters):
cell = table.cell(i+1,2)
textFrame = cell.text_frame
run = textFrame.paragraphs[0].add_run()
font = run.font
font.name = 'TSTAR PRO'
font.size = Pt(16)
run.text = presenters[i][:-1]
font.color.theme_color = 5
i += 1
#saves the ppt
prs.save("Agenda Slide.pptx")
#File is deleted from the desktop if present there
if os.path.exists("Agenda Slide.txt"):
os.remove("Agenda Slide.txt")
#File moved to the desktop
shutil.move(os.getcwd() + "\\Agenda Slide.pptx", os.path.join(os.environ["HOMEPATH"], "Desktop") + "\\Agenda Slide.pptx")
现在,正如我所说,当我从我的 IDE 启动它时,它就像一个魅力。
我在 python 脚本中打开和读取的文件是由我的 C# 程序写入的。这是一种将变量从我的 C# 传输到我的 Python 的低技术方式,我知道这不是很漂亮.. xD
在我的 c# 程序中,这是我启动脚本的方式:
private void runPythonScript()
{
Process p = new Process(); // create process (i.e., the python program
p.StartInfo.FileName = @"C:\Python34\python.exe";
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = getRessourcesDir() + "createAgendaPPT\\createAgendaPPT\\createAgendaPPT.py";
p.Start();
p.WaitForExit();
p.Close();
}
函数getRessourcesDir() 只是返回解决方案中Ressources 文件夹的目录。
总结一下:
- 尝试从 C# 启动 python 脚本
- Pythonscript 将在我的 IDE 中正确运行,但不能通过启动 .py 文件来运行。
我需要帮助: 如何使 python 脚本可从它的 .py 文件启动,以及如何从 C# 启动它。
如果有任何不清楚或遗漏的地方,请告诉我,我会编辑我的帖子并更正它。
【问题讨论】:
-
尝试从
cmdshell 命令行运行它。 -
@martineau 当我尝试这样做时,它会给出错误“C:\python34\python.exe: can't open file 'C:\Users\aalbaek-nt\Desktop\createAgendaPPT.py ': [Errno 2] 没有这样的文件或目录。”但我绝对确定我给了它正确的目录......
-
您应该能够通过输入
dir C:\Users\aalbaek-nt\Desktop\createAgendaPPT.py来确定是否给它一些有效的东西。单独输入dir C:\Users\aalbaek-nt\Desktop将列出文件夹中的所有文件。 -
@martineau 当我运行该命令时,它显示“找不到文件”,所以我猜测快捷方式可能是它失败的地方?桌面上的文件
createAgendaPPT是真实文件的快捷方式会不会有问题?这样做的原因是我使用 `os.getcwd()' 函数来定位我正在使用的一些文件的目录,并且通过移动文件我担心它不会再定位到正确的目录了。跨度> -
Yoy 应该能够右键单击桌面上
xxx的图标并选择“属性”,这将导致出现一个包含对象信息的对话框。如果它是一个快捷方式,那么它会这样说,并且还会向您显示真实文件的位置——这就是您要使用的路径。