Hướng dẫn python-docx font size - Kích thước phông chữ python-docx

Tôi đang tạo một tài liệu MS Word từ một mẫu sử dụng Python-docx và tôi cần một hàm cho phép tôi định dạng văn bản trong cùng một đoạn.

Tôi đã tìm thấy đoạn trích này trong một chủ đề khác làm việc cho tôi:

class Text: def __init__(self, text, bold=False, italic=False, color=None): self.text = text self.bold = bold self.italic = italic self.color = color def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color

Điều này được gọi từ một chức năng khác theo cách này:

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))])

Bây giờ tôi muốn thực hiện kích thước loại phông chữ và phông chữ. Cách nào tốt nhất để làm điều đó?

Đã giải quyết theo cách này:

class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size

Và gọi nó:

add_text([Text('This is an ', bold=False, italic=False, color=None, name='Calibri', size=Pt(12)), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00), name='Calibri', size=Pt(12))])

Màu sắc, như một chủ đề, vượt ra ngoài đối tượng add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 7; Phông chữ màu chỉ là nơi đầu tiên mà nó xuất hiện. Theo đó, nó có suy nghĩ sâu sắc hơn một chút so với bình thường vì chúng tôi sẽ muốn tái sử dụng cùng một đối tượng và giao thức để chỉ định màu sắc trong các bối cảnh khác; Nó có ý nghĩa để tạo ra một giải pháp chung sẽ mang lại dự kiến ​​tái sử dụng.

Có ba nguồn lịch sử để rút ra từ API này.

  1. W: RPR/W: Phần tử màu. Điều này được sử dụng theo mặc định khi áp dụng màu trực tiếp vào văn bản hoặc khi đặt màu văn bản của một kiểu. Điều này tương ứng với thuộc tính Font.Color (không may, không may). Yếu tố này hỗ trợ màu RGB, màu chủ đề và màu hoặc sắc thái của một màu chủ đề.
  2. Các yếu tố W: RPR/W14: TextFill. Điều này được sử dụng bởi Word cho văn bản ưa thích như gradient và hiệu ứng bóng. Điều này tương ứng với thuộc tính Font.fill.
  3. UI màu phông chữ PowerPoint. Điều này có vẻ như là một sự thỏa hiệp hợp lý giữa hai người trước đó, cho phép truy cập trực tiếp vào các tùy chọn màu phổ biến trong khi giữ cửa mở cho các hoạt động font.fill được thêm vào sau nếu được yêu cầu.

Giao thức ứng cử viên

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 8 có thuộc tính phông chữ:

>>> from docx import Document >>> from docx.text.run import Font, Run >>> run = Document().add_paragraph().add_run() >>> isinstance(run, Run) True >>> font = run.font >>> isinstance(font, Font) True

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 9 có thuộc tính màu chỉ đọc, trả về đối tượng class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 0:

>>> from docx.dml.color import ColorFormat >>> color = font.color >>> isinstance(font.color, ColorFormat) True >>> font.color = 'anything' AttributeError: can't set attribute

class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 0 có thuộc tính class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 2 chỉ đọc và đọc/ghi class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 3, class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 4 và class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 5 thuộc tính.

class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 6 Trả về một trong MSO_COLOR_TYPE.RGB, MSO_COLOR_TYPE.THEME, MSO_COLOR_TYPE.AUTO hoặc class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 7, cái sau biểu thị phông chữ không có màu được áp dụng trực tiếp:

class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 8 Trả về một đối tượng class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 9 khi loại là mSO_color_type.rgb. Nó cũng có thể báo cáo giá trị RGBColor khi loại là MSO_Color_Type.theme, vì màu RGB cũng có thể có trong trường hợp đó. Theo thông số kỹ thuật, giá trị màu RGB bị bỏ qua khi một màu chủ đề được chỉ định, nhưng Word ghi giá trị RGB hiện tại của màu chủ đề cùng với tên màu chủ đề (ví dụ: Accent1,) khi gán màu chủ đề; Có lẽ là một giá trị thuận tiện cho một trình duyệt tệp để sử dụng. Giá trị của .type phải được tư vấn để xác định xem giá trị RGB có hoạt động hay là một người giỏi nhất hay không:

>>> font.color.type RGB (1) >>> font.color.rgb RGBColor(0x3f, 0x2c, 0x36)

Gán giá trị class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 9 cho class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 8 khiến class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 6 trở thành MSO_COLOR_TYPE.RGB:

>>> font.color.type None >>> font.color.rgb = RGBColor(0x3f, 0x2c, 0x36) >>> font.color.type RGB (1) >>> font.color.rgb RGBColor(0x3f, 0x2c, 0x36)

add_text([Text('This is an ', bold=False, italic=False, color=None, name='Calibri', size=Pt(12)), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00), name='Calibri', size=Pt(12))]) 3 Trả về một thành viên của MSO_Theme_Color_index khi loại là MSO_Color_Type.Theme:MSO_THEME_COLOR_INDEX when type is MSO_COLOR_TYPE.THEME:

>>> font.color.type THEME (2) >>> font.color.theme_color ACCENT_1 (5)

Việc gán một thành viên của MSO_Theme_color_index cho add_text([Text('This is an ', bold=False, italic=False, color=None, name='Calibri', size=Pt(12)), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00), name='Calibri', size=Pt(12))]) 3 khiến class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 6 trở thành MSO_Color_Type.theme:MSO_THEME_COLOR_INDEX to add_text([Text('This is an ', bold=False, italic=False, color=None, name='Calibri', size=Pt(12)), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00), name='Calibri', size=Pt(12))]) 3 causes class Text: def __init__(self, text, bold=False, italic=False, color=None, name=None, size=None): self.text = text self.bold = bold self.italic = italic self.color = color self.name = name self.size = size def add_text(textitems): p = doc.add_paragraph('') for t in textitems: r = p.add_run(t.text) if t.bold: r.bold = True if t.italic: r.italic = True if isinstance(t.color, RGBColor): r.font.color.rgb = t.color if t.name: r.font.name = t.name if t.size: r.font.size = t.size 6 to become MSO_COLOR_TYPE.THEME:

>>> font.color.type RGB (1) >>> font.color.theme_color = MSO_THEME_COLOR.ACCENT_2 >>> font.color.type THEME (2) >>> font.color.theme_color ACCENT_2 (6)

Thuộc tính add_text([Text('This is an ', bold=False, italic=False, color=None, name='Calibri', size=Pt(12)), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00), name='Calibri', size=Pt(12))]) 6 có thể được sử dụng để chọn một màu hoặc màu của màu chủ đề. Gán giá trị 0,1 tạo ra màu sáng hơn 10% (một tông màu); Gán -0.1 tạo ra màu tối hơn 10% (một bóng râm):

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 0

Mẫu vật XML¶

Đoạn đường cơ sở không có màu phông chữ:

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 1

Đoạn văn với màu RGB được áp dụng trực tiếp:

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 2

Chạy với màu chủ đề được áp dụng trực tiếp:

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 3

Chạy với 40% Tint của văn bản 2 Màu chủ đề:

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 4

Chạy với 25% màu của Accent 2 Màu sắc chủ đề:

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 5

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

add_text([Text('This is an ', bold=False, italic=False, color=None), Text('example', bold=True, italic=False, color=RGBColor(0xFF, 0x00, 0x00))]) 6

Chủ đề