在将.docx文件转录为新的.docx文档时,如何在段落中插入<w:numPr>和相关数据?

时间:2020-03-21 01:46:51

标签: python python-docx

for src_paragraph in src_doc.paragraphs:
                src_paragraph_format = src_paragraph.paragraph_format
                # print(src_paragraph.text)

                # Handle Headers/Footers                                        Headers not implemented
                # 
                sections = trgt_doc.sections                                    # there's only 1 section
                section = sections[0]
                footer = section.footer                                         # get the footer section of the section
                paragraph = footer.paragraphs[0]                                # footer has 1 paragraph
                paragraph.text = f'{page_number} \t\t\t {printed_time_stamp}'   

                # Transcribe paragraph settings - Build the target
                #
                trgt_paragraph = trgt_doc.add_paragraph(style = src_paragraph.style ) 

                if src_paragraph._p.pPr.numPr is not None:
                    print('\n <w:pStyle> :', src_paragraph._p.pPr.pStyle)
                    print ('<w:numPr> :', src_paragraph._p.pPr.numPr)
                    print ('\t<w:ilvl> :', src_paragraph._p.pPr.numPr.ilvl)
                    print ('\t<w:numId> :', src_paragraph._p.pPr.numPr.numId)
                    print('\n', src_paragraph.text)

                trgt_paragraph_format = trgt_paragraph.paragraph_format
                trgt_paragraph.style.name = src_paragraph.style.name
                trgt_paragraph_format.left_indent = src_paragraph_format.left_indent  # inherited from style hierarchy
                trgt_paragraph_format.right_indent = src_paragraph_format.right_indent 
                # print('S_INDENT -------|', src_paragraph_format.left_indent)
                # print('T_INDENT -------|', trgt_paragraph_format.left_indent)
                trgt_paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
                trgt_paragraph_format.widow_control = True
                font = trgt_paragraph.style.font
                font.name = 'Times'
                font.size = Pt(11)

我正在将Word文件转录为具有相同信息的相似文档。内容,但有修改和补充。我通过遍历源段落然后创建目标段落/运行来构建目标文件。

除了捕获数字项目符号外,大多数都已完成。我可以捕获 ,但目前还不知道如何将这些值添加到每个目标段落中。

这是我的第一个使用.docx数据的项目,我正在研究中。

1 个答案:

答案 0 :(得分:0)

在尝试仅将我生成的目标.docx文件插入时,我尝试了 方法https://python.developreference.com/article/15889882/How+to+add+line+numbers+to+a+docx+document+section+using+python-docx

https://stackoverflow.com/questions/38400208/how-to-add-line-numbers-to-a-docx-document-section-using-python-docx

# Generate new Target file from Source File
for src_paragraph in src_doc.paragraphs:
    src_paragraph_format = src_paragraph.paragraph_format
    # Get Target section(s)
    sections = trgt_doc.sections
    section = sections[0]
    sectPr = section._sectPr
    lnNumType = OxmlElement('w:lnNumType')
    lnNumType.set('fooAttrib', '42')
    sectPr.append(lnNumType)
    print('STUBB')

这里是行号,而不是轮廓样式编号列表。我只是想做一个初始插入以查看它是否有效;没有。

# Add Numbered List to Target paragraphs.
# Isolate the number bulleted paragraphs 
if src_paragraph._p.pPr.numPr:
   # SOURCE XML Paragraphs containing numPr
   print('--------------------------------------------')
   print('TEXT_SRC', src_paragraph.text,'\n')
   print('SRC ParXML \n', src_paragraph._p.xml)
   print('--------------------------------------------')     

我可以通过这种方式在源.docx中找到小数;诀窍就是让它进入我正在生成的目标中。