python:如何通过一些重复的关键字拆分大的.docx文件

时间:2018-04-16 09:03:04

标签: python-3.x python-docx

我想根据一些重复的关键字将.docx文件拆分成小的.docx文件。比如,我的文件有数据...

ABCD:

kjndlndflf jfnkajnfak kjnflanlkl fwefagasdg

ABCD:

adnclwk asdafvdfv afersdf

ABCD:

sdfqerf rfwer ERER

我想将两个“ABCD”中的内容复制到名为ABCD1 ... ABCD(n)的不同文件中。

我的代码是

def writeFiles():
  '''User defined string to search and split the doc, Doc will get
     split after searching this string as a starting line for the 
     doc till one line before the string appers again'''

    feature = input (" Please provide the Feature ID \n : ")  /*asking keyword from user

  #Asking user to select desired file 
    print ("Please select file to split")

    path=get_path();  /*function for file browsing



    i =1;
    document = Document(docx=path)      
    for paragraph in document.paragraphs:
      if feature in paragraph.text:
        document.add_paragraph()
        document.add_page_break()
        name = feature+repr(i)+".docx"
        document.save(name);
        i= i+1;

如果文件具有关键字“N”次但不在两个重复关键字之间复制数据,则代码正在制作和复制完整数据“N”个文件。

下面的代码现在用关键字拆分文件,但格式,字体和表格不会出现

try:
    if feature in paragraph.text:
      name = feature + repr(i) + ".docx"
      i = i + 1
      doc.add_paragraph(paragraph.text.split(feature)[0])
      doc.save(name);
      doc = Document()
      doc.add_paragraph(paragraph.text.split(feature)[1])

else:
      doc.add_paragraph(paragraph.text)

请帮帮我......

0 个答案:

没有答案