c# - Python 脚本无法从 C# 或运行 .py 启动
问题描述
在您将此标记为转发之前,请阅读我的问题 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# 启动它。
如果有任何不清楚或遗漏的地方,请告诉我,我将编辑我的帖子并更正它。
解决方案
Try running python script from a command window.
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");
推荐阅读
- python - pandas groupby 并在不同类型之间使用数字
- c - 有没有办法将信息保存到结构的指针
- angular - 使用 webpack 和 gulp 将大型角度模块拆分为 AEM 项目中的多个模块
- javascript - 正则表达式用括号替换所有出现的文本
- javascript - 在标签集中使用数据表时防止 R .rmd html 文档跳过
- ruby - Ruby 脚本无法打开 SQLite3 文件,但只能在作为守护进程运行时打开
- java - UnboundID LDAP 和 Squid 代理
- parse-server - 为什么带有聚合的日期查询在解析服务器中不起作用?
- javascript - 将计算属性与 Vue Chart js 一起使用时的无限循环
- c# - 按下按钮后如何设置DialogResult?