使用python

时间:2017-01-06 14:19:35

标签: python-2.7 parsing pdf pypdf pdf-parsing

我正在尝试编写一个程序,为需求 - 代码 - 测试

提供跟踪矩阵

我无法从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

这种搜索的目的是说,我得到顶级要求" {CSD-PROS-PLN-003}"然后我将从这个要求中搜索其他pdf / code / test以获得派生的需求/实现/测试。

由于

0 个答案:

没有答案
相关问题