পাইথনে textwrap মডিউলটি সাদা টেক্সট ফরম্যাট ও র্যাপ করার জন্য ব্যবহৃত হয়। ইনপुट প্যারাগ্রাফের লাইন ব্রেক অ্যাডজাস্ট করে টেক্সট ফরম্যাট করার কিছু অপশন রয়েছে।
এই মডিউল ব্যবহার করতে কোডে textwrap মডিউল ইম্পোর্ট করতে হবে।
import textwrap
TextWrapper ইনস্ট্যান্স আট্রিবিউটগুলো (কনস্ট্রাক্টরের) নিচের মতো:
| Sr.No. | Attribute & Description | |
|---|---|---|
| 1 | width লাইনের সর্বোচ্চ দৈর্ঘ্য। ডিফল্ট ৭০। |
|
| 2 | expand_tabs যদি এই আট্রিবিউটটি true হয়, সব ট্যাব স্পেসে রূপান্তরিত হবে। ডিফল্ট True। |
|
| 3 | tabsize expand_tabs true হলে ট্যাবসাইজ সেট করতে সাহায্য করে। ডিফল্ট ৮। |
|
| 4 | replace_whitespace সকল হোয়াইটস্পেস ক্যারেক্টার সিঙ্গল স্পেসে রূপান্তরিত হবে যদি true থাকে। ডিফল্ট True। |
|
| 5 | drop_whitespace র্যাপিং এর পর শুরু এবং শেষের হোয়াইটস্পেস ড্রপ হবে। ডিফল্ট True। |
|
| 6 | initial_indent |
র্যাপ করা টেক্সটের প্রথম লাইনের শুরুতে দেওয়া স্ট্রিং যোগ হবে। ডিফল্ট ''। |
| 7 | subsequent_indent |
র্যাপ করা টেক্সটের সকল লাইনের শুরুতে দেওয়া স্ট্রিং যোগ হবে। ডিফল্ট ''। |
| 8 | placeholder |
আউটপুট ট렁কেট হলে শেষে এই স্ট্রিং যোগ হবে। ডিফল্ট […]। |
| 9 | max_lines |
র্যাপিং এর পর সর্বোচ্চ কত লাইন থাকবে তা নির্ধারণ করে। None হলে লিমিট নেই। ডিফল্ট None। |
| 10 | break_long_words |
দীর্ঘ শব্দগুলো দिये गये width-এ ফিট করার জন্য ভাঙবে। ডিফল্ট True। |
| 11 | break_on_hyphens |
হাইফেনের পরে টেক্সট র্যাপ করা যাবে কমপাউন্ড শব্দের জন্য। ডিফল্ট True। |
টেক্সট র্যাপিং মেথড
Textwrap মডিউলে কিছু মেথড রয়েছে:
Module (textwrap.wrap(text, width = 70, **kwargs)) -
এই মেথডটি ইনপুট প্যারাগ্রাফ র্যাপ করে। লাইন উইডথ ব্যবহার করে কন্টেন্ট র্যাপ করে। ডিফল্ট লাইন উইডথ ৭০। এটি লাইনগুলোর একটি লিস্ট রিটার্ন করে যেখানে সব র্যাপ করা লাইন সংরক্ষিত থাকে।
Module (textwrap.fill(text, width = 70, **kwargs)) -
fill() মেথড wrap মেথডের মতোই, কিন্তু এটি লিস্ট জেনারেট করে না। এটি একটি স্ট্রিং জেনারেট করে। নির্দিষ্ট উইডথ অতিক্রম হলে নিউ লাইন ক্যারেক্টার যোগ করে।
Module (textwrap.shorten(text, width, **kwargs)) -
এই মেথডটি স্ট্রিং সংক্ষিপ্ত বা ট렁কেট করে। ট렁কেশন এর পর টেক্সটের দৈর্ঘ্য নির্দিষ্ট উইডথের সমান হয়। শেষে […] যোগ করে।
উদাহরণ কোড
import textwrap python_desc = """Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). This tutorial gives enough understanding on Python programming language.""" my_wrap = textwrap.TextWrapper(width = 40) wrap_list = my_wrap.wrap(text=python_desc) for line in wrap_list: print(line) single_line = """Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.""" print('\n\n' + my_wrap.fill(text = single_line)) short_text = textwrap.shorten(text = python_desc, width=150) print('\n\n' + my_wrap.fill(text = short_text))
আউটপুট
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). This tutorial gives enough understanding on Python programming language. Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. Python is a general-purpose interpreted, interactive, object-oriented, and high- level programming language. It was created by Guido van Rossum [...]