在另一个内部调用python脚本

时间:2015-05-27 09:33:15

标签: python

我已经看到了一些与我的问题有关的问题,但未能得到答案。

在我的程序中,我有一个需要转换为PDF的.txt文件。 我遇到了同样的脚本https://code.activestate.com/recipes/189858-python-text-to-pdf-converter/

我已将此导入我的程序,但我不知道如何调用并传递我的txt文件,以便将其转换为PDF。

.txt到.pdf转换器脚本名称是txttopdf.py我已将其导入为import txttopdf并且它存在于同一目录中

我的程序的最后一部分是尝试将.txt转换为.pdf,但它给我一个语法错误。 以下是我的计划         import sqlite3         进口平台         导入系统         进口口         进口重新         进口时间         导入smtplib         进口mimetypes         import txttopdf         从datetime导入日期时间         来自email.mime.multipart导入MIMEMultipart         来自电子邮件导入编码器         来自email.message导入消息

    from email.mime.text import MIMEText
    ipstr = "unknown"
    errorstr = "unknown"
    gtstr = "unknown"

    print "reading the file"

    linuxpath = raw_input("Enter the path")

    txt_file = open(linuxpath,"r")   
    countlines = 0
    if os.stat("lastline.txt").st_size == 0:
    for line in open(linuxpath):                                        
      pattern = re.compile('(([2][5][0-5]\.)|([2][0-4][0-9]\.)|([0-1]?[0-9]?[0-9]\.)){3}'+'(([2][5][0-5])|([2][0-4][0-9])|([0-1]?[0-9]?[0-9]))|[\d.]+|\:\:\d|[\w\.]+')
   #([\d.]+)[\s-]+\s+"([A-Z]+)\s+(.+?)"\s+([\s\d]+)')\[([\d\/A-Za-z: -]+)\]
   iprgex = pattern.search(line)
   #print "hi"
   countlines = countlines + 1
   if iprgex:
      ips = iprgex.start()
      ipe = iprgex.end()
      ipstr = line[ips:ipe]
      #print "hi again"
      #print ipstr

   pattern = re.compile('[\d]+\/[A-Za-z]+\/[\d]+')
   #('\[([\d\/A-Za-z: -]+)\]')
   datergex = pattern.search(line)
   #print "hi"
   if datergex:
      dates = datergex.start()
      datee = datergex.end()
      datestr = line[dates:datee]
      #countlines = countlines + 1
      #print "hi again"
      #print datestr
      monthstr = datestr[3:6]
      #print monthstr
      if monthstr == "Jan":
         date_chnge = datestr.replace("Jan","01")
      elif monthstr == "Feb":
         date_chnge = datestr.replace("Feb","02")
      elif monthstr == "Mar":
         date_chnge = datestr.replace("Mar","03")
         #print "am here"
        #print datestr
  elif monthstr == "Apr":
         date_chnge = datestr.replace("Apr","04")
  elif monthstr == "May":
         date_chnge = datestr.replace("May","05")
  elif monthstr == "Jun":
         date_chnge = datestr.replace("Jun","06")
  elif monthstr == "Jul":
         date_chnge = datestr.replace("Jul","07")
  elif monthstr == "Aug":
         date_chnge = datestr.replace("Aug","08")
  elif monthstr == "Sep":
         date_chnge = datestr.replace("Sep","09")
  elif monthstr == "Oct":
         date_chnge = datestr.replace("Oct","10")
  elif monthstr == "Nov":
         date_chnge = datestr.replace("Nov","11")
  elif monthstr == "Dec":
         date_chnge = datestr.replace("Dec","12")

   #print date_chnge
   dt_day = date_chnge[0:2]
   dt_month = date_chnge[3:5]
   dt_year = date_chnge[6:]

   new_date = dt_year + '-' + dt_month + '-' + dt_day

   pattern = re.compile('\:[\d]+\:[\d]+\:[\d]+')
   #('\[([\d\/A-Za-z: -]+)\]')
   timergex = pattern.search(line)
   #print "hi"
   if timergex:
      times = timergex.start()
      timee = timergex.end()
      timestr = line[times:timee]
      #countlines = countlines + 1
      #print "hi again"
      #print timestr
   extract_time = timestr[1:]
   datestring = new_date + ' ' + extract_time
   dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
   #print dt.year, dt.month, dt.day

   pattern = re.compile('"([A-Z]+)\s+(.+?)"|"\-"')
   getrgex = pattern.search(line)
   #print line
   if getrgex:
      gts = getrgex.start()
      gte = getrgex.end()
      gtstr = line[gts:gte]
      #countlines = countlines + 1
      #print "hi again"
      #print gtstr

   pattern = re.compile('200|401|403|404|412|500|302')
   errorrgex = pattern.search(line)
   #print "hi"
   if errorrgex:
      errors = errorrgex.start()
      errore = errorrgex.end()
      errorstr = line[errors:errore]
      #countlines = countlines + 1
      #print "hi again"
      #print errorstr

   file = open('parse1.txt','a')
   file.write(ipstr + datestr +timestr + gtstr + errorstr + "\n")
    #Analysing the get request
   print countlines
   #print ipstr,dt,gtstr,errorstr
   with open('ALLINONE.txt','r') as f:
        for cheatsheetline in f:
    indexvalue = gtstr.strip().find(cheatsheetline.strip())
    #print gtstr
    if indexvalue > 0:
        #print indexvalue
        file = open('CAUTION.txt','a')
            file.write(ipstr + datestr +timestr + gtstr + errorstr + "\n")
    #break
    file.close()    

   lastlinefile = open('lastline.txt','w+')
   lastlinefile.write(line)
   #this part should convert the txt file CAUTION.txt to PDF
   #txttopdf.main()
   txttopdf CAUTION.txt  

1 个答案:

答案 0 :(得分:2)

最简单的方法是通过subprocess.Popen

示例:

import sys
from subprocess import Popen, PIPE,, STDOUT


PYTEXT2PDF = "/path/to/pytext2pdf"


def convert(filename):
    print("Converting {} to PDF".format(filename))

    p = Popen(
        [sys.executable, PYTEXT2PDF, filename],
        stdout=PIPE, stderr=STDOUT
    )

    stdout, _ = p.communicate()

    print(stdout)


convert("filename.txt")

从外表看; pyText2Pdf会将文本文件转换为PDF,并将输出文件命名为与输入文件相同的“basenaem”,扩展名为.pdf