【问题标题】:Python script not launchable from C# or by running .pyPython 脚本无法从 C# 或运行 .py 启动
【发布时间】: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# 启动它。

如果有任何不清楚或遗漏的地方,请告诉我,我会编辑我的帖子并更正它。

【问题讨论】:

  • 尝试从cmd shell 命令行运行它。
  • @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 的图标并选择“属性”,这将导致出现一个包含对象信息的对话框。如果它是一个快捷方式,那么它会这样说,并且还会向您显示真实文件的位置——这就是您要使用的路径。

标签: c# python process


【解决方案1】:

尝试从命令窗口运行 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");

【讨论】:

  • 来自评论:您能否为您的答案添加更多细节?您发布的代码没有任何意义...
  • 感谢您的帮助,但它似乎也不起作用.. 当我将您的代码复制并粘贴到我的函数中并将最后一行中的“xxxx”更改为:“createAgendaPPT”时,当我调用该函数时没有任何反应。
  • 您的 python 脚本必须在您的构建目录中(Debug 或 Release)。
  • @T.Aalbaek 这对我有用,实际上不知道出了什么问题。尝试在调试目录中打开一个 cmd 窗口并手动运行您的 python 脚本。
猜你喜欢
  • 1970-01-01
  • 2019-07-31
  • 2010-10-17
  • 2020-08-29
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 2019-06-25
  • 2011-04-14
相关资源
最近更新 更多