在将其标记为转发之前,请阅读我的问题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文件夹的目录。
总而言之:
我需要什么帮助: 如何使python脚本可从.py文件启动,以及如何从C#启动。
如果有任何不清楚或遗漏的地方,请告诉我,我将对其进行编辑并更正。
答案 0 :(得分:0)
尝试从命令窗口运行python脚本。
Process cmdProcess;
StreamWriter cmdStreamWriter;
StreamReader cmdStreamReader;
cmdProcess = new Process();
cmdProcess.StartInfo.FileName = "cmd.exe";
cmdProcess.StartInfo.UseShellExecute = false;
cmdProcess.StartInfo.CreateNoWindow = true;
cmdProcess.StartInfo.StandardOutputEncoding = Encoding.ASCII;
cmdProcess.StartInfo.RedirectStandardOutput = true;
cmdProcess.StartInfo.RedirectStandardInput = true;
cmdProcess.Start();
cmdStreamWriter = cmdProcess.StandardInput;
cmdStreamReader = cmdProcess.StandardOutput;
cmdStreamWriter.WriteLine("python xxxx.py");