Bạn có thể thêm màu bằng Python không?

Một cách đơn giản, không cần thư viện để in văn bản in đậm trong Python là đặt một chuỗi

python -m pip install simple-colors
9 đã cho trong chuỗi thoát đặc biệt như vậy.
import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
0

Chúng ta sẽ thảo luận về giải pháp Pythonic nhất trong Phương pháp 2 trong bài viết này

Bạn có thể thay đổi văn bản của mình thành đậm, nghiêng và gạch chân bằng Python. Bạn không chỉ có thể thử nghiệm với kiểu mã của mình mà còn có thể thay đổi màu sắc của mã với sự trợ giúp của các gói và mô-đun cụ thể trong Python.

Thú vị. Phải không?

Có nhiều cách khác nhau để làm điều này. Đến cuối hướng dẫn này, bạn sẽ được trang bị tất cả các công cụ để chơi theo phong cách mã của mình

Hướng dẫn liên quan

  • Làm cách nào để in văn bản đậm trong Python?
  • Làm cách nào để in văn bản nghiêng trong Python?
  • Làm cách nào để in văn bản được gạch chân bằng Python?
  • Làm cách nào để in văn bản màu bằng Python?

Vì vậy, không chậm trễ hơn nữa, hãy để các trò chơi bắt đầu

📹Video hướng dẫn

Cách in văn bản màu bằng Python


Xem video này trên YouTube

Phương pháp 1. Sử dụng Gói màu đơn giản

Python cung cấp một gói ngoạn mục có tên là màu đơn giản cho phép chúng tôi hiển thị văn bản có màu trong thiết bị đầu cuối

  • Gói
    import simple_colors
    
    # colored text
    print['Normal:', simple_colors.blue['Welcome Finxter!']]
    # BOLD and colored text
    print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
    # BOLD and Underlined and colored text
    print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
    1 bao gồm các màu như xanh dương, đen, xanh lục, đỏ tươi, đỏ, vàng và lục lam
  • Bạn cũng có thể định dạng văn bản của mình theo nhiều kiểu khác nhau như đậm, mờ, nghiêng, sáng, gạch chân , đảo ngược và nhấp nháy được bao gồm trong .

Vì gói

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
2 không phải là một phần của thư viện chuẩn của Python nên bạn cần cài đặt nó trước khi sử dụng. Để cài đặt gói simple_color, hãy sao chép đoạn mã sau vào thiết bị đầu cuối của bạn

pip install simple-colors

hoặc,

python -m pip install simple-colors

Sau khi bạn đã cài đặt thành công mô-đun, bạn có thể làm theo cú pháp được cung cấp trong ví dụ bên dưới để tùy chỉnh/tạo kiểu cho mã của mình

Ví dụ. Ví dụ sau minh họa cách bạn có thể thêm màu sắc, định dạng và làm cho văn bản được in đậm hoặc thậm chí gạch chân bằng cách sử dụng mô-đun

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
1.

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]

đầu ra

Tiếng hoan hô. Cái nhìn đó thật tuyệt vời

Giải trình

  • Để hiển thị văn bản có màu, bạn phải gọi hàm màu cụ thể từ thư viện màu đơn giản và chuyển văn bản vào hàm này. Ví dụ: câu lệnh in đầu tiên hiển thị văn bản màu xanh lam với sự trợ giúp của hàm
    import simple_colors
    
    # colored text
    print['Normal:', simple_colors.blue['Welcome Finxter!']]
    # BOLD and colored text
    print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
    # BOLD and Underlined and colored text
    print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
    4. Tương tự, khi bạn gọi phương thức
    import simple_colors
    
    # colored text
    print['Normal:', simple_colors.blue['Welcome Finxter!']]
    # BOLD and colored text
    print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
    # BOLD and Underlined and colored text
    print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
    5, nó sẽ tạo ra một dòng chữ màu xanh lá cây
  • Để thêm kiểu cùng với màu sắc cho văn bản, chúng tôi đã sử dụng phương thức màu cụ thể và chuyển loại định dạng [đậm, nghiêng, gạch chân, v.v. ] làm đối số cho hàm màu. Để thêm nhiều kiểu định dạng vào văn bản của mình, bạn chỉ cần chuyển các kiểu định dạng làm đối số cho hàm bằng cách đóng gói chúng trong một danh sách

Phương pháp 2. Sử dụng Trình tự thoát ANSI

Cách Pythonic nhất để in văn bản màu trong Python là kèm theo một chuỗi đã cho

python -m pip install simple-colors
9 trong chuỗi thoát đặc biệt như.
import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
0

Đây là một ví dụ tối thiểu

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
2

Bạn có thể tự mình thử điều này trong sổ tay Jupyter tương tác của chúng tôi

🧩 Tương tác. Tự mình thử trong Google Colab

Tiếp theo, hãy đi sâu vào một số giải thích để xem lý do tại sao điều này hoạt động

Một số thiết bị đầu cuối hỗ trợ khả năng chuyển các chuỗi thoát duy nhất để sửa đổi tông màu, màu sắc và hình thức của nội dung được in. Các chuỗi thoát này được gọi là Các chuỗi thoát ANSI được đặt tên theo tiêu chuẩn ANSI cho biết việc sử dụng chúng.

Do đó, bạn có thể sử dụng chuỗi thoát ANSI tích hợp để tạo nội dung có màu. Đây là bảng phân tích các chuỗi ANSI mà bạn có thể sử dụng trong mã của mình

  • Trình giữ chỗ xác định xem màu phải được áp dụng cho chính văn bản hay cho nền văn bản-
    • 38;5;
    • 48;5;
  • Tiếp theo là mã màu từ 0 đến 255

Thí dụ. Ví dụ sau minh họa cách bạn có thể sử dụng Escape-Sequences để in đồng thời văn bản có màu, được gạch chân và in đậm

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
3

đầu ra

💡 LƯU Ý. Mã

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
8 dùng để kết thúc định dạng chữ in đậm. Nếu bạn quên thêm chuỗi mã ANSI để đính kèm dòng mã cụ thể, các câu lệnh sau cũng sẽ được in đậm

Phương pháp 3. Sử dụng mô-đun termcolor

Trong Python,

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
9 là một mô-đun được sử dụng cho định dạng màu ANSII

Mô-đun đi kèm với các thuộc tính khác nhau cho các thiết bị đầu cuối khác nhau và các thuộc tính định dạng văn bản nhất định. Nó cũng bao gồm nhiều màu văn bản khác nhau như xanh dương, đỏ và xanh lá cây cũng như các điểm nổi bật của văn bản như đỏ tươi, lục lam và trắng

💡 Lưu ý. Mô-đun

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
9 không phải là một phần của thư viện chuẩn của Python. Vì vậy, bạn cần phải cài đặt nó trước khi sử dụng nó. Để cài đặt mô-đun
import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
9, hãy sao chép đoạn mã sau trên thiết bị đầu cuối của bạn

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
8

Sau khi cài đặt mô-đun, hãy hình dung cách bạn có thể sử dụng nó để in văn bản màu

Thí dụ

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
9

Phương pháp 4. Sử dụng gói Colorama

Các chuỗi ký tự thoát ANSI từ lâu đã được sử dụng để tạo văn bản đầu cuối có màu và định vị con trỏ trên Unix và Mac

Colorama cũng làm cho tính năng này hoạt động trên Windows bằng cách bao gói thiết bị xuất chuẩn, loại bỏ các chuỗi ANSI mà nó tìm thấy [sẽ xuất hiện dưới dạng gobbledygook ở đầu ra] và chuyển đổi chúng thành lệnh gọi win32 thích hợp để sửa đổi trạng thái của thiết bị đầu cuối. Trên các nền tảng khác, Colorama không làm gì cả

  • Ghi chú
    • Trên các nền tảng khác, Colorama không làm gì cả. Trong Windows, gọi
      import simple_colors
      
      # colored text
      print['Normal:', simple_colors.blue['Welcome Finxter!']]
      # BOLD and colored text
      print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
      # BOLD and Underlined and colored text
      print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
      22 sẽ chuyển chuỗi thoát ANSI của bất kỳ nội dung nào được gửi đến thiết bị xuất chuẩn hoặc thiết bị xuất chuẩn và thay thế nó bằng các cuộc gọi Win32
    • Để ngừng sử dụng Colorama trước khi chương trình của bạn thoát, hãy gọi
      import simple_colors
      
      # colored text
      print['Normal:', simple_colors.blue['Welcome Finxter!']]
      # BOLD and colored text
      print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
      # BOLD and Underlined and colored text
      print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
      23. Nó sẽ khôi phục thiết bị xuất chuẩn và thiết bị xuất chuẩn về giá trị ban đầu của chúng

Nguồn. https. //pypi. tổ chức/dự án/colorama/

python -m pip install simple-colors
6 không phải là một phần của thư viện chuẩn của Python, nên bạn phải cài đặt nó bằng lệnh sau trong thiết bị đầu cuối của mình trước khi có thể sử dụng nó

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
3

Hãy cùng xem một ví dụ để hiểu cách chúng ta có thể sử dụng mô-đun Colorama để in đậm văn bản trong Python

ví dụ 1

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
4

đầu ra

ví dụ 2. Sử dụng gói

python -m pip install simple-colors
6 cùng với mô-đun
import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
9

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
7

đầu ra

Phương pháp 5. Sử dụng Mô-đun màu

được tô màu là một thư viện Python rất đơn giản được sử dụng để tô màu và định dạng văn bản trong thiết bị đầu cuối. Vì nó không phải là một mô-đun tích hợp, trước tiên bạn phải cài đặt nó bằng pip.

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
27

Ví dụ sử dụng

python -m pip install simple-colors
0

đầu ra

Phương pháp 6. Tạo đối tượng Html

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
28 bao gồm một hàm 
import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
29 tương thích [càng nhiều càng tốt] với hàm print[] tích hợp. Nó cũng hỗ trợ màu sắc và định dạng

HTML có thể được sử dụng để chứng minh rằng một chuỗi chứa định dạng dựa trên HTML. Do đó, đối tượng HTML nhận ra các thẻ cần thiết cho chữ đậm, chữ nghiêng và chữ gạch chân.

import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
30,
import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
31 và
import simple_colors

# colored text
print['Normal:', simple_colors.blue['Welcome Finxter!']]
# BOLD and colored text
print['BOLD: ', simple_colors.green['Welcome Finxter!', 'bold']]
# BOLD and Underlined and colored text
print['BOLD and Underlined: ', simple_colors.red['Welcome Finxter!', ['bold', 'underlined']]]
32

ví dụ 1

python -m pip install simple-colors
1

đầu ra

ví dụ 2

Vì tất cả các thẻ HTML được ánh xạ tới các lớp từ biểu định kiểu, bạn cũng có thể chỉ định kiểu cho thẻ tùy chỉnh

python -m pip install simple-colors
2

đầu ra

Sự kết luận

Cuối cùng chúng ta đã chinh phục được nghệ thuật in chữ đậm bằng Python. Chúng tôi không chỉ học cách in đậm văn bản mà còn học cách định kiểu mã bằng cách sử dụng màu sắc và các kiểu định dạng khác như gạch chân và in nghiêng. Tôi hy vọng bài viết này đã giúp bạn

Hãy theo dõi và đăng ký để có nhiều bài viết thú vị hơn

  • Bạn có muốn nhanh chóng thành thạo Python IDE phổ biến nhất không?
  • Khóa học này sẽ đưa bạn từ người mới bắt đầu đến chuyên gia về PyCharm trong ~90 phút
  • Đối với bất kỳ nhà phát triển phần mềm nào, điều quan trọng là phải thành thạo IDE, viết, kiểm tra và gỡ lỗi mã chất lượng cao mà không tốn nhiều công sức

Tham gia Lớp học nâng cao PyCharm ngay bây giờ và thành thạo PyCharm vào ngày mai

Shubham Sayon

Tôi là một người tạo nội dung và Blogger Python chuyên nghiệp. Tôi đã xuất bản nhiều bài báo và tạo các khóa học trong một khoảng thời gian. Hiện tại tôi đang làm việc với tư cách là một freelancer toàn thời gian và tôi có kinh nghiệm trong các lĩnh vực như Python, AWS, DevOps và Networking

Bạn có thể sử dụng mã màu trong python không?

Để làm cho một số văn bản của bạn dễ đọc hơn, bạn có thể sử dụng mã thoát ANSI để thay đổi màu của văn bản đầu ra trong chương trình python của mình . Một trường hợp sử dụng tốt cho việc này là làm nổi bật các lỗi. Các mã thoát được nhập ngay vào câu lệnh in.

Bạn có thể in màu trong python không?

Cách Pythonic nhất để in văn bản màu trong Python là đặt một chuỗi s đã cho trong chuỗi thoát đặc biệt như. in["\033[38;5;4m"].

Làm cách nào để cài đặt mô-đun màu trong python?

Cài đặt mô-đun termcolor. cài đặt thuật ngữ pip3
Nhập lớp màu từ termcolor. từ termcolor nhập màu
Sử dụng các phương pháp được cung cấp, dưới đây là một ví dụ. in [màu ['xin chào', 'đỏ'], màu ['thế giới', 'xanh']]

Mã màu trong python là gì?

Mô-đun hằng số màu Python

Chủ Đề