我看到了几个与我的问题相关的问题,但无法得到答案。
在我的程序中,我有一个需要转换为 PDF 的 .txt 文件。 我遇到了执行相同操作的脚本,https://code.activestate.com/recipes/189858-python-text-to-pdf-converter/
我已将其导入到我的程序中,但我不确定如何调用和传递我的 txt 文件以便将其转换为 PDF。
.txt 到 .pdf 转换器脚本名称是 txttopdf.py 我已将其导入为 import txttopdf 并且它存在于同一目录中
我程序的最后一部分试图将 .txt 转换为 .pdf,但它给我一个语法错误。 下面是我的程序 导入sqlite3 导入平台 导入系统 导入操作系统 重新进口 导入时间 导入 smtplib 导入模仿类型 导入 txttopdf 从日期时间导入日期时间 从 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
请您参考如下方法:
最简单的方法是通过 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。
