Hướng dẫn python-docx delete section - phần xóa python-docx

Tôi đang cố gắng loại bỏ các phần ngắt khỏi một tài liệu từ. Đối với điều này, tôi đang cố gắng loại bỏ thuộc tính SECTPR khỏi XML được tạo ra thông qua Python-docx. Đây là XML được tạo ra:

<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex wp14">
  <w:body>
    <w:p w14:paraId="0F1E22A8" w14:textId="1CB95B52" w:rsidR="006F7C29" w:rsidRDefault="00B46A6B">
      <w:pPr>
        <w:sectPr w:rsidR="006F7C29">
          <w:pgSz w:w="11906" w:h="16838"/>
          <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
          <w:cols w:space="708"/>
          <w:docGrid w:linePitch="360"/>
        </w:sectPr>
      </w:pPr>
      <w:r>
        <w:t>math</w:t>
      </w:r>
    </w:p>
    <w:p w14:paraId="3FE55637" w14:textId="789D24FC" w:rsidR="003660CC" w:rsidRPr="003660CC" w:rsidRDefault="003660CC" w:rsidP="008F17C5"/>
    <w:sectPr w:rsidR="003660CC" w:rsidRPr="003660CC" w:rsidSect="008F17C5">
      <w:type w:val="evenPage"/>
      <w:pgSz w:w="11906" w:h="16838"/>
      <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
      <w:cols w:space="708"/>
      <w:docGrid w:linePitch="360"/>
    </w:sectPr>
  </w:body>
</w:document>

Tôi đã viết mã sau để xóa Sectpr:


def identifySbr(doc):
    allp=len(doc.paragraphs)
    document_xml = doc.element.xml


    for i in range(0,allp):
        c = doc.paragraphs[i]._p.xpath("./w:pPr/w:sectPr")

        if len(c)>0:
            ca = doc.paragraphs[i]._p.xpath("./w:pPr/w:sectPr")[0]
            ca.attrib.pop(qn("w:sectPr"))

Nhưng tôi đang gặp lỗi này:

ca.attrib.pop(qn("w:sectPr"))
  File "src\lxml\etree.pyx", line 2449, in lxml.etree._Attrib.pop
KeyError: '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}sectPr'

Có ai có thể vui lòng giúp tôi giải quyết điều này?

Word hỗ trợ khái niệm của một phần, có cài đặt bố cục trang riêng biệt. Đây là cách, ví dụ, một tài liệu có thể chứa một số trang trong bố cục chân dung và những trang khác trong cảnh quan. Phần ngắt được thực hiện hoàn toàn khác với các dòng, trang và cột. Cái trước thêm một yếu tố <w:pPr><w:sectPr> vào đoạn cuối trong phần mới. Phần sau chèn một phần tử <w:br> trong một lần chạy.

Phần cuối cùng trong một tài liệu được chỉ định bởi phần tử <w:sectPr> xuất hiện dưới dạng con cuối cùng của phần tử ____10. Mặc dù phần tử này là tùy chọn, nhưng có vẻ như từ tạo nó cho tất cả các tệp. Vì hầu hết các tệp chỉ có một phần duy nhất, nên trường hợp phổ biến nhất là trong đó đây là phần tử duy nhất <w:sectPr>.

Các phần bổ sung được chỉ định bởi một phần tử


def identifySbr(doc):
    allp=len(doc.paragraphs)
    document_xml = doc.element.xml


    for i in range(0,allp):
        c = doc.paragraphs[i]._p.xpath("./w:pPr/w:sectPr")

        if len(c)>0:
            ca = doc.paragraphs[i]._p.xpath("./w:pPr/w:sectPr")[0]
            ca.attrib.pop(qn("w:sectPr"))

2 trong đoạn cuối của phần. Bất kỳ nội dung nào trong đoạn đó là một phần của phần được xác định bởi phần tử ____99 của nó. Phần tiếp theo bắt đầu với đoạn sau.

Khi một phần ngắt được chèn bằng cách sử dụng từ UI, các bước sau xảy ra:

  1. Phần tử <w:sectPr> tiếp theo được sao chép và thêm vào đoạn hiện tại. (Sẽ rất thú vị khi xem điều gì xảy ra khi đoạn đó đã có phần tử <w:sectPr>.)
  2. Một đoạn mới được chèn vào sau đoạn văn hiện tại. Văn bản xảy ra sau khi vị trí con trỏ được chuyển đến đoạn mới.
  3. Loại bắt đầu (ví dụ: trang tiếp theo) của phần tử <w:sectPr> tiếp theo được thay đổi để phản ánh loại được người dùng chọn từ UI.

Hành vi từ

  • Một đoạn chứa phần ngắt phần (phần tử) không tạo ra ¶ glyph trong từ UI.
  • Chỉ báo phá vỡ phần/dòng kép xuất hiện trực tiếp sau văn bản của đoạn trong đó xuất hiện. Nếu phần Break đoạn văn không có văn bản, dòng chỉ báo sẽ xuất hiện ngay sau dấu đoạn của đoạn trước.

Trước và sau khi phân tích Bur

Tài liệu cơ bản chứa hai đoạn văn:

<w:body>
  <w:p>
    <w:r>
      <w:t>Paragraph 1</w:t>
    </w:r>
  </w:p>
  <w:p>
    <w:r>
      <w:t>Paragraph 2</w:t>
    </w:r>
  </w:p>
  <w:sectPr>
    <w:pgSz w:w="12240" w:h="15840"/>
    <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800"
             w:header="720" w:footer="720" w:gutter="0"/>
    <w:cols w:space="720"/>
    <w:docGrid w:linePitch="360"/>
  </w:sectPr>
</w:body>

Phần trang lẻ được chèn trước dấu đoạn trong đoạn 1:

<w:body>
  <w:p>
    <w:pPr>
      <w:sectPr>
        <w:pgSz w:w="12240" w:h="15840"/>
        <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800"
                 w:header="720" w:footer="720" w:gutter="0"/>
        <w:cols w:space="720"/>
        <w:docGrid w:linePitch="360"/>
      </w:sectPr>
    </w:pPr>
    <w:r>
      <w:t>Paragraph 1</w:t>
    </w:r>
  </w:p>
  <w:p/>
  <w:p>
    <w:r>
      <w:t>Paragraph 2</w:t>
    </w:r>
  </w:p>
  <w:sectPr w:rsidR="00F039D0" w:rsidSect="006006E7">
    <w:type w:val="oddPage"/>
    <w:pgSz w:w="12240" w:h="15840"/>
    <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800"
             w:header="720" w:footer="720" w:gutter="0"/>
    <w:cols w:space="720"/>
    <w:docGrid w:linePitch="360"/>
  </w:sectPr>
</w:body>

UI hiển thị trống ¶ Đánh dấu ở vị trí đầu tiên của trang tiếp theo mới. Chỉ báo phá vỡ phần xuất hiện trực tiếp sau văn bản đoạn 1, không có dấu hiệu can thiệp.

Phần chẵn được chèn vào ký tự đầu tiên trong đoạn 2:

<w:body>
  <w:p>
    <w:r>
      <w:t>Paragraph 1</w:t>
    </w:r>
  </w:p>
  <w:p>
    <w:pPr>
      <w:sectPr>
        <w:type w:val="oddPage"/>
        <w:pgSz w:w="12240" w:h="15840"/>
        <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800"
                 w:header="720" w:footer="720" w:gutter="0"/>
        <w:cols w:space="720"/>
        <w:docGrid w:linePitch="360"/>
      </w:sectPr>
    </w:pPr>
  </w:p>
  <w:p>
    <w:r>
      <w:lastRenderedPageBreak/>
      <w:t>Paragraph 2</w:t>
    </w:r>
  </w:p>
  <w:sectPr>
    <w:type w:val="evenPage"/>
    <w:pgSz w:w="12240" w:h="15840"/>
    <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800"
             w:header="720" w:footer="720" w:gutter="0"/>
    <w:cols w:space="720"/>
    <w:docGrid w:linePitch="360"/>
  </w:sectPr>
</w:body>

Liệt kê

Wd_section_start¶

Bí danh: WD_SectionWD_SECTION

WDSectionStart liệt kê trên MSDN

Liên tục (0) Phần liên tục ngắt.New_Column (1) Phần mới phần Cột bị phá vỡ.New_Page (2) Phần mới Break.Even_Page (3) thậm chí các trang Break.odd_Page (4) Trang lẻ Break.

Đoạn trích lược đồ

<xsd:complexType name="CT_PPr">  <!-- denormalized -->
  <xsd:sequence>
    <!-- 34 others ... -->
    <xsd:element name="sectPr"    type="CT_SectPr"    minOccurs="0"/>
    <xsd:element name="pPrChange" type="CT_PPrChange" minOccurs="0"/>
  </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="CT_SectPr">  <!-- denormalized -->
  <xsd:sequence>
    <xsd:choice minOccurs="0" maxOccurs="6"/>
      <xsd:element name="headerReference" type="CT_HdrFtrRef"/>
      <xsd:element name="footerReference" type="CT_HdrFtrRef"/>
    </xsd:choice>
    <xsd:element name="footnotePr"      type="CT_FtnProps"      minOccurs="0"/>
    <xsd:element name="endnotePr"       type="CT_EdnProps"      minOccurs="0"/>
    <xsd:element name="type"            type="CT_SectType"      minOccurs="0"/>
    <xsd:element name="pgSz"            type="CT_PageSz"        minOccurs="0"/>
    <xsd:element name="pgMar"           type="CT_PageMar"       minOccurs="0"/>
    <xsd:element name="paperSrc"        type="CT_PaperSource"   minOccurs="0"/>
    <xsd:element name="pgBorders"       type="CT_PageBorders"   minOccurs="0"/>
    <xsd:element name="lnNumType"       type="CT_LineNumber"    minOccurs="0"/>
    <xsd:element name="pgNumType"       type="CT_PageNumber"    minOccurs="0"/>
    <xsd:element name="cols"            type="CT_Columns"       minOccurs="0"/>
    <xsd:element name="formProt"        type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="vAlign"          type="CT_VerticalJc"    minOccurs="0"/>
    <xsd:element name="noEndnote"       type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="titlePg"         type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="textDirection"   type="CT_TextDirection" minOccurs="0"/>
    <xsd:element name="bidi"            type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="rtlGutter"       type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="docGrid"         type="CT_DocGrid"       minOccurs="0"/>
    <xsd:element name="printerSettings" type="CT_Rel"           minOccurs="0"/>
    <xsd:element name="sectPrChange"    type="CT_SectPrChange"  minOccurs="0"/>
  </xsd:sequence>
  <xsd:attribute name="rsidRPr"  type="ST_LongHexNumber"/>
  <xsd:attribute name="rsidDel"  type="ST_LongHexNumber"/>
  <xsd:attribute name="rsidR"    type="ST_LongHexNumber"/>
  <xsd:attribute name="rsidSect" type="ST_LongHexNumber"/>
</xsd:complexType>

<xsd:complexType name="CT_HdrFtrRef">
  <xsd:attribute  ref="r:id"                  use="required"/>
  <xsd:attribute name="type" type="ST_HdrFtr" use="required"/>
</xsd:complexType>

<xsd:simpleType name="ST_HdrFtr">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="even"/>
    <xsd:enumeration value="default"/>
    <xsd:enumeration value="first"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="CT_SectType">
  <xsd:attribute name="val" type="ST_SectionMark"/>
</xsd:complexType>

<xsd:simpleType name="ST_SectionMark">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="nextPage"/>
    <xsd:enumeration value="nextColumn"/>
    <xsd:enumeration value="continuous"/>
    <xsd:enumeration value="evenPage"/>
    <xsd:enumeration value="oddPage"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="CT_PageSz">
  <xsd:attribute name="w"      type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="h"      type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="orient" type="ST_PageOrientation"/>
  <xsd:attribute name="code"   type="ST_DecimalNumber"/>
</xsd:complexType>

<xsd:simpleType name="ST_PageOrientation">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="portrait"/>
    <xsd:enumeration value="landscape"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="CT_PageMar">
  <xsd:attribute name="top"    type="ST_SignedTwipsMeasure" use="required"/>
  <xsd:attribute name="right"  type="s:ST_TwipsMeasure"     use="required"/>
  <xsd:attribute name="bottom" type="ST_SignedTwipsMeasure" use="required"/>
  <xsd:attribute name="left"   type="s:ST_TwipsMeasure"     use="required"/>
  <xsd:attribute name="header" type="s:ST_TwipsMeasure"     use="required"/>
  <xsd:attribute name="footer" type="s:ST_TwipsMeasure"     use="required"/>
  <xsd:attribute name="gutter" type="s:ST_TwipsMeasure"     use="required"/>
</xsd:complexType>

<xsd:simpleType name="ST_SignedTwipsMeasure">
  <xsd:union memberTypes="xsd:integer s:ST_UniversalMeasure"/>
</xsd:simpleType>

<xsd:complexType name="CT_Columns">
  <xsd:sequence minOccurs="0">
    <xsd:element name="col" type="CT_Column" maxOccurs="45"/>
  </xsd:sequence>
    <xsd:attribute name="equalWidth" type="s:ST_OnOff"/>
    <xsd:attribute name="space"      type="s:ST_TwipsMeasure"/>
    <xsd:attribute name="num"        type="ST_DecimalNumber"/>
    <xsd:attribute name="sep"        type="s:ST_OnOff"/>
</xsd:complexType>

<xsd:complexType name="CT_Column">
  <xsd:attribute name="w"     type="s:ST_TwipsMeasure"/>
  <xsd:attribute name="space" type="s:ST_TwipsMeasure"/>
</xsd:complexType>