2

I have seen a couple of question related to my issue but haven't been able to get an answer.

In my program I have a .txt file that needs to be converted to PDF. I came across this script that does the same, https://code.activestate.com/recipes/189858-python-text-to-pdf-converter/

I have imported this into my program, but I am not sure how to call and pass my txt file so that it converts it to PDF.

.txt to .pdf converter script name is txttopdf.py i have imported it as import txttopdf and it is present in the same directory

Last part of my program is trying to convert the .txt to .pdf but it gives me a Syntax error. Below is my program import sqlite3 import platform import sys import os import re import time import smtplib import mimetypes import txttopdf from datetime import datetime from email.mime.multipart import MIMEMultipart from email import encoders from email.message import 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 Answer 1

2

The easiest way to do this is via subprocess.Popen:

Example:

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")

By the looks of it; pyText2Pdf will convert the Text file to PDF and name the output file the same "basenaem" as the input file with the extension of .pdf.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.