2017-05-17 15 views
1

私はMS Word文書の作成を自動化しています。完了したら、PDFとして保存し、他の/外部のPDFページをWord文書のPDFバージョンに挿入する必要があります。これを行うために、私は自分のページ上のWord文書にマーカー(例: "[pdfGoesHere]")を残すことを計画していました。python-docxを使用してMS Wordの段落の開始(および終了)ページを取得します。

新しいPDFページを挿入/置換するには、マーカーがどのページにあるかを知る必要があります。 python-docxは、段落がどのページ番号を開始(および終了)するかを決定する方法を持っていますか?私はpython-docx documentationを読んできましたが、これは何もないようです。私はすべての段落を巡回し、私が興味を持っている段落を見つけることができますが、段落のページ番号を取得する決定論的な方法を見つけることはできません。

私は見落としているこれを行う方法はありますか?そうでない場合は、PDFページを挿入するという主要な目的を達成するための他の提案がありますか?

答えて

0

私は@scannyによって与えられたフィードバックを感謝しています。 python-docxでこれを行う方法がないので、とにかく文書をPDFに変換しているので、Word文書をPDFに変換した後にpdfminerを使用してページ番号を取得することにしました。このコードは長いかもしれないが、それはこの後

import re 
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter 
from pdfminer.layout import LAParams 
from pdfminer.pdfpage import PDFPage 
from cStringIO import StringIO 

def xmlToLines(xml): 
    text = ''.join(xml) 
    return text.split('\n') 

#Convert a PDF found at the 'path' and turns it into XML lines 
#path is the full path directory to the PDF file you're reading from 
def convert_pdf_to_xml(path): 
    rsrcmgr = PDFResourceManager() 
    retstr = StringIO() 
    codec = 'utf-8' 
    laparams = LAParams() 
    device = XMLConverter(rsrcmgr, retstr, codec=codec, laparams=laparams) 
    fp = file(path, 'rb') 
    interpreter = PDFPageInterpreter(rsrcmgr, device) 
    password = "" 
    maxpages = 0 
    caching = True 
    pagenos=set() 

    print 'Converting following file from PDF to XML: \n - ' + str(path) 
    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True): 
     interpreter.process_page(page) 

    text = retstr.getvalue() 
    lines = xmlToLines(text) 

    #Close out pdf and I/O's 
    fp.close() 
    device.close() 
    retstr.close() 

    return lines 

#returns a list of every page number where the field marker is found in the PDF 
def getPagesWithField(wordPdfPath, field): 
    lines = convert_pdf_to_xml(wordPdfPath) 
    page_regex = r'page id="[0-9]*"' 
    t_regex = r'<text font=' 
    pagesFound = [] 
    text = '' 
    field = field.replace('<','&').replace('>','&') 
    for i in range(len(lines)): 
     #If it's a new page line, increment to the new page 
     if len(re.findall(page_regex, lines[i])) > 0: 
      page = int(re.findall(r'[0-9]{1,}', lines[i])[0]) 
      #print 'page: ' + str(page) 
     #If it's the end of a line 
     elif lines[i] == '<text>': 
      #print "Text: " + text 
      #check if the collected text is the field you're looking for 
      if field in text: 
       pagesFound.append(page) 
      text = '' 
     #If it's a line with a text character, add it to text 
     elif len(re.findall(t_regex, lines[i])) > 0: 
      text = str(text + re.findall(r'>[^\r\n]*</text>',lines[i])[0][1]) 

    pagesFound = list(set(pagesFound)) 
    pagesFound.sort()  
    return pagesFound 

を仕事を取得し、PyPDF2は、私が上のマーカーを配置している場合

1

短い答えはいいえです。ページ番号はレンダリング時に決定され、使用可能なフォントなどの理由でデバイスに依存します。

この答えは、詳細の多くを持っている: Page number python-docx

+0

はどんなオプションが私に開くですかマージ/簡単なPDFのページの挿入のために使用することができますページブレークを使用するページ自体はどれですか?あるいは、フォントのレンダリングなどで問題が解決しないのでしょうか? – Jed

+0

'python-docx'自体からページ番号を取得する方法はありません。私はあなたがページがオーバーフローしないことを確かめることができるマニュアルに改ページを置いた場合、あなた自身でそれらを数えることができると思います。しかし、あなたのコンテンツが可変であれば、それは問題になりがちです。 – scanny