Hướng dẫn python-pptx line spacing - khoảng cách dòng python-pptx

Những gì tôi cố gắng làm là thay đổi khoảng cách cho mỗi lần chạy trong hàm:

def add_direction_arrows(self):
    global junc_file, junc_file_name
    match_colors_to_type = {"White": RGBColor(255, 255, 255), "Yellow": RGBColor(238, 233, 23)}
    arrows_placeholders = {"NORTH_ARROWS": self.NO.LAN.Organize_arrows_order(),
                           "SOUTH_ARROWS": self.SO.LAN.Organize_arrows_order(),
                           "EAST_ARROWS": self.EA.LAN.Organize_arrows_order(),
                           "WEST_ARROWS": self.WE.LAN.Organize_arrows_order()}
    for slide in junc_file.slides:
        for shape in slide.shapes:
            if shape.name in arrows_placeholders.keys():
                text_frame = shape.text_frame
                text_frame.clear()
                arrows_list = arrows_placeholders[shape.name]
                for arrow in arrows_list:
                    text_frame = shape.text_frame
                    p = text_frame.paragraphs[0]
                    run = p.add_run()
                    run.text = arrow[0] * arrow[1]
                    font = run.font
                    font.bold = False
                  ➜ font.spacing = -10
                    font.size = Pt(49.9)
                    font.color.rgb = match_colors_to_type[arrow[2]]
    junc_file.save(junc_file_name)

Tôi không chắc liệu thư viện Python-PPTX có bao gồm tùy chọn này không, nhưng nếu không, tôi có những tùy chọn nào khác? Có thể tự thêm nó không? Có thể là một cách để 'nhấn nút' trong PowerPoint theo cách thủ công? cảm ơn!

Đã hỏi ngày 7 tháng 3 năm 2021 lúc 8:21Mar 7, 2021 at 8:21

Hướng dẫn python-pptx line spacing - khoảng cách dòng python-pptx

Theo tài liệu

def _set_char_spacing(cell):
    tc = cell._tc
    txBody = tc.get_or_add_txBody()
    if not len(txBody.p_lst) > 0:
      return cell

    p = txBody.p_lst[0] # NOTE: Using index 0 just for demo purposes
    r = p.r_lst[0] # NOTE: Using index 0 just for demo purposes
    if not r.rPr:
      rPr = r._add_rPr()

    spacing = Emu(40)
    rPr.attrib['spc'] = str(spacing)
    return cell
0 không hỗ trợ khoảng cách.

Bạn có thể đi sâu vào cách thư viện hoạt động trong nội bộ và có thể thêm hỗ trợ, nhưng tôi nghi ngờ đây thực sự là một lựa chọn cho bạn.

Tất nhiên bạn có thể chỉnh sửa các tệp PowerPoint bằng tay sau để thay đổi điều này (hoặc thậm chí sử dụng VBA).

Đã trả lời ngày 7 tháng 3 năm 2021 lúc 8:34Mar 7, 2021 at 8:34

Matthiasmatthiasmatthias

1091 Huy hiệu bạc6 Huy hiệu đồng1 silver badge6 bronze badges

5

Có vẻ như không có hỗ trợ cho như vậy. Là một cách giải quyết, tôi đã sử dụng đoạn trích sau đây để đạt được cùng một hình dạng hình dạng. Có lẽ bạn có thể xem xét nếu nó hoạt động giống nhau cho các lần chạy văn bản bằng cách thay thế hai dòng đầu tiên để có được khung văn bản txbody.

def _set_char_spacing(cell):
    tc = cell._tc
    txBody = tc.get_or_add_txBody()
    if not len(txBody.p_lst) > 0:
      return cell

    p = txBody.p_lst[0] # NOTE: Using index 0 just for demo purposes
    r = p.r_lst[0] # NOTE: Using index 0 just for demo purposes
    if not r.rPr:
      rPr = r._add_rPr()

    spacing = Emu(40)
    rPr.attrib['spc'] = str(spacing)
    return cell

Như một từ chối trách nhiệm, P_LST và R_LST là các danh sách sẽ yêu cầu lặp lại.

Đã trả lời ngày 20 tháng 8 năm 2021 lúc 8:55Aug 20, 2021 at 8:55

Khoảng cách đoạn văn

Một đoạn có ba thuộc tính khoảng cách. Nó có thể có khoảng cách phân tách nó với đoạn trước, khoảng cách tách nó ra khỏi đoạn sau và khoảng cách dòng nhỏ hơn hoặc lớn hơn khoảng cách dòng đơn mặc định.

Giao thức ứng cử viên

Nhận và đặt không gian trước/sau:

>>> paragraph = TextFrame.add_paragraph()
>>> paragraph.space_before
0
>>> paragraph.space_before = Pt(6)
>>> paragraph.space_before
76200
>>> paragraph.space_before.pt
6.0

Một đoạn hiện có mà bằng cách nào đó có không gian trước hoặc sau khi được đặt theo các dòng (

def _set_char_spacing(cell):
    tc = cell._tc
    txBody = tc.get_or_add_txBody()
    if not len(txBody.p_lst) > 0:
      return cell

    p = txBody.p_lst[0] # NOTE: Using index 0 just for demo purposes
    r = p.r_lst[0] # NOTE: Using index 0 just for demo purposes
    if not r.rPr:
      rPr = r._add_rPr()

    spacing = Emu(40)
    rPr.attrib['spc'] = str(spacing)
    return cell
1) sẽ trả về giá trị độ dài (0) cho thuộc tính đó.

Nhận và đặt khoảng cách dòng:

>>> paragraph = TextFrame.add_paragraph()
>>> paragraph.line_spacing
1.0
>>> isinstance(paragraph.line_spacing, float)
True
>>> paragraph.line_spacing = Pt(18)
>>> paragraph.line_spacing
228600
>>> isinstance(paragraph.line_spacing, Length)
True
>>> isinstance(paragraph.line_spacing, int)
True
>>> paragraph.line_spacing.pt
18.0

  • Giá trị mặc định cho

    def _set_char_spacing(cell):
        tc = cell._tc
        txBody = tc.get_or_add_txBody()
        if not len(txBody.p_lst) > 0:
          return cell
    
        p = txBody.p_lst[0] # NOTE: Using index 0 just for demo purposes
        r = p.r_lst[0] # NOTE: Using index 0 just for demo purposes
        if not r.rPr:
          rPr = r._add_rPr()
    
        spacing = Emu(40)
        rPr.attrib['spc'] = str(spacing)
        return cell
    
    2 là 1.0 (dòng).

  • Các đơn vị của giá trị trả về có thể được phân biệt theo loại của nó. Trong thực tế, các đơn vị cũng có thể được phân biệt theo cường độ. Các dòng được trả lại dưới dạng phao nhỏ. Giá trị điểm được trả về dưới dạng giá trị độ dài.

    Nếu loại là phân biệt đối xử thuận tiện nhất, thì biểu thức isinstance (line_spaces, float) sẽ đóng vai trò là một vị ngữ IS_LINES hiệu quả.

    Trong thực tế, các giá trị dòng sẽ hiếm khi lớn hơn 3.0 và thật khó để tưởng tượng một khoảng cách dòng hữu ích dưới 1 pt (12700). Nếu cường độ là phân biệt đối xử thuận tiện nhất, 256 có thể được coi là giá trị ngưỡng an toàn.

MS API

LineruleFterDetermines cho dù khoảng cách dòng sau dòng cuối cùng trong mỗi đoạn được đặt thành một số điểm hoặc đường cụ thể. Read/write.LineruleBeforedEtermines xem khoảng cách dòng trước dòng đầu tiên trong mỗi đoạn được đặt thành một số điểm hoặc dòng cụ thể. Read/write.LineruleWithIndetermines xem khoảng cách dòng giữa các dòng cơ sở được đặt thành một số điểm hoặc đường cụ thể. Đọc/Write.SpaceAfterReturns hoặc đặt lượng không gian sau dòng cuối cùng trong mỗi đoạn của văn bản được chỉ định, theo điểm hoặc dòng. Read/Write.SpacebeForereturns hoặc đặt lượng không gian trước dòng đầu tiên trong mỗi đoạn của văn bản được chỉ định, theo điểm hoặc dòng. Đọc/Viết.SpacewithInreturns hoặc đặt lượng không gian giữa các dòng cơ sở trong văn bản được chỉ định, theo điểm hoặc dòng. Đọc viết.

Hành vi PowerPoint

  • UI dường như không cho phép khoảng cách dòng được đặt theo đơn vị dòng, mặc dù API XML và MS cho phép nó được chỉ định như vậy.

Mẫu vật XML

Khoảng cách đoạn văn mặc định:

<a:p>
  <a:r>
    <a:t>Paragraph with default spacinga:t>
  a:r>
a:p>

Khoảng cách trước = 6 pt:

<a:p>
  <a:pPr>
    <a:spcBef>
      <a:spcPts val="600"/>
    a:spcBef>
  a:pPr>
  <a:r>
    <a:t>Paragraph spacing before = 6pta:t>
  a:r>
a:p>

Khoảng cách sau = 12 pt:

<a:p>
  <a:pPr>
    <a:spcAft>
      <a:spcPts val="1200"/>
    a:spcAft>
  a:pPr>
  <a:r>
    <a:t>Paragraph spacing after = 12pta:t>
  a:r>
a:p>

Khoảng cách dòng = 24 pt:

<a:p>
  <a:pPr>
    <a:lnSpc>
      <a:spcPts val="2400"/>
    a:lnSpc>
  a:pPr>
  <a:r>
    <a:t>Paragraph line spacing = 24pta:t>
  a:r>
  <a:br/>
  <a:r>
    <a:t>second linea:t>
  a:r>
a:p>

Khoảng cách dòng = 2 dòng:

<a:p>
  <a:pPr>
    <a:lnSpc>
      <a:spcPct val="200000"/>
    a:lnSpc>
  a:pPr>
  <a:r>
    <a:t>Paragraph line spacing = 2 linea:t>
  a:r>
  <a:br/>
  <a:r>
    <a:t>second linea:t>
  a:r>
a:p>

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

<xsd:complexType name="CT_TextParagraph">
  <xsd:sequence>
    <xsd:element name="pPr"        type="CT_TextParagraphProperties" minOccurs="0"/>
    <xsd:group    ref="EG_TextRun" minOccurs="0" maxOccurs="unbounded"/>
    <xsd:element name="endParaRPr" type="CT_TextCharacterProperties" minOccurs="0"/>
  xsd:sequence>
xsd:complexType>

<xsd:complexType name="CT_TextParagraphProperties">
  <xsd:sequence>
    <xsd:element name="lnSpc"       type="CT_TextSpacing"             minOccurs="0"/>
    <xsd:element name="spcBef"      type="CT_TextSpacing"             minOccurs="0"/>
    <xsd:element name="spcAft"      type="CT_TextSpacing"             minOccurs="0"/>
    <xsd:choice minOccurs="0">       
      <xsd:element name="buClrTx"   type="CT_TextBulletColorFollowText"/>
      <xsd:element name="buClr"     type="CT_Color"/>
    xsd:choice>
    <xsd:choice minOccurs="0">       
      <xsd:element name="buSzTx"    type="CT_TextBulletSizeFollowText"/>
      <xsd:element name="buSzPct"   type="CT_TextBulletSizePercent"/>
      <xsd:element name="buSzPts"   type="CT_TextBulletSizePoint"/>
    xsd:choice>
    <xsd:choice minOccurs="0">       
      <xsd:element name="buFontTx"  type="CT_TextBulletTypefaceFollowText"/>
      <xsd:element name="buFont"    type="CT_TextFont"/>
    xsd:choice>
    <xsd:choice minOccurs="0">       
      <xsd:element name="buNone"    type="CT_TextNoBullet"/>
      <xsd:element name="buAutoNum" type="CT_TextAutonumberBullet"/>
      <xsd:element name="buChar"    type="CT_TextCharBullet"/>
      <xsd:element name="buBlip"    type="CT_TextBlipBullet"/>
    xsd:choice>
    <xsd:element name="tabLst"      type="CT_TextTabStopList"         minOccurs="0"/>
    <xsd:element name="defRPr"      type="CT_TextCharacterProperties" minOccurs="0"/>
    <xsd:element name="extLst"      type="CT_OfficeArtExtensionList"  minOccurs="0"/>
  xsd:sequence>
  <xsd:attribute name="marL"         type="ST_TextMargin"/>
  <xsd:attribute name="marR"         type="ST_TextMargin"/>
  <xsd:attribute name="lvl"          type="ST_TextIndentLevelType"/>
  <xsd:attribute name="indent"       type="ST_TextIndent"/>
  <xsd:attribute name="algn"         type="ST_TextAlignType"/>
  <xsd:attribute name="defTabSz"     type="ST_Coordinate32"/>
  <xsd:attribute name="rtl"          type="xsd:boolean"/>
  <xsd:attribute name="eaLnBrk"      type="xsd:boolean"/>
  <xsd:attribute name="fontAlgn"     type="ST_TextFontAlignType"/>
  <xsd:attribute name="latinLnBrk"   type="xsd:boolean"/>
  <xsd:attribute name="hangingPunct" type="xsd:boolean"/>
xsd:complexType>

<xsd:complexType name="CT_TextSpacing">
  <xsd:choice>
    <xsd:element name="spcPct" type="CT_TextSpacingPercent"/>
    <xsd:element name="spcPts" type="CT_TextSpacingPoint"/>
  xsd:choice>
xsd:complexType>

<xsd:complexType name="CT_TextSpacingPercent">
  <xsd:attribute name="val" type="ST_TextSpacingPercentOrPercentString" use="required"/>
xsd:complexType>

<xsd:simpleType name="ST_TextSpacingPercentOrPercentString">
  <xsd:union memberTypes="ST_TextSpacingPercent s:ST_Percentage"/>
xsd:simpleType>

<xsd:simpleType name="ST_TextSpacingPercent">
  <xsd:restriction base="ST_PercentageDecimal">
    <xsd:minInclusive value="0"/>
    <xsd:maxInclusive value="13200000"/>
  xsd:restriction>
xsd:simpleType>

<xsd:simpleType name="ST_Percentage">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="-?[0-9]+(\.[0-9]+)?%"/>
  xsd:restriction>
xsd:simpleType>

<xsd:complexType name="CT_TextSpacingPoint">
  <xsd:attribute name="val" type="ST_TextSpacingPoint" use="required"/>
xsd:complexType>

<xsd:simpleType name="ST_TextSpacingPoint">
  <xsd:restriction base="xsd:int">
    <xsd:minInclusive value="0"/>
    <xsd:maxInclusive value="158400"/>
  xsd:restriction>
xsd:simpleType>