我正在尝试编写一个程序,该程序为需求-代码-测试提供跟踪矩阵
为此,我无法从 PDF 中逐行读取 PDF 中的需求标签。
下面是我试过的程序。
import os, sys, time
import sys
import glob
import xlwt
sys.path.insert(0,'C:/Python27/xlwt-0.7.5')
import pyPdf
from StringIO import StringIO
import docx
req_path_py = os.path.dirname(os.path.abspath(__file__)) +"\\Requirement\\"
req_list = glob.glob("%s/*.pdf" %req_path_py)
def getPDFContent(path):
content = ""
# Load PDF into pyPDF
pdf = pyPdf.PdfFileReader(file(path, "rb"))
# Iterate pages
for i in range(0, pdf.getNumPages()):
# Extract text from page and add to content
content += pdf.getPage(i).extractText() + "\n"
# Collapse whitespace
content = " ".join(content.replace(u"\xa0", " ").strip().split())
return content
def topReq():
global req_path_py, req_list
with open("traceMetrix.txt","w") as txt:
txt.write("CSD ID \tSRD ID \tSDD ID\tCODE ID\tTEST ID\n")
pdfContent = StringIO(getPDFContent(req_list[0]).encode("ascii", "ignore"))
for line in pdfContent:
if '{CSD' in line:
txt.write(line)
if __name__ == "__main__":
topReq()
PDF 的内容如下所示
Trace:
{CSD-PROS-PLN-001}
Abc shall do abc
Trace:
{CSD-PROS-PLN-002}
Abc shall do abc
Trace:
{CSD-PROS-PLN-003}
Abc shall do abc
有时,某些内容会在表 sy 下面的条目位于表内
Trace:
{CSD-PROS-PLN-003}
Abc shall do abc
这种搜索的目的是说,我将顶级 req 作为“{CSD-PROS-PLN-003}”然后我将搜索其他 pdf / code/test 以获取来自该要求的派生要求 / implementation / test。
谢谢