1. Overview
In the article How to Quickly Generate Lorem lpsum Text in Microsoft Word and PowerPoint, we introduced how to quickly generate a specified number of Lorem ipsum texts in Word and PowerPoint. In fact we can do the same thing using Python.
Python has countless wheels, and one of them called python-lorem
can help us. The function of this wheel is to generate Lorem ipsum text. python-lorem
is rich in functions and easy to use. It provides three ways to generate words, sentences, and paragraphs. At the same time, it also provides a thesaurus customization function. If you are patient enough, you can even use it to write novels.
2. Install
Execute the following command to install python-lorem
.
pip install python-lorem
3. Generate Words
The get_word
function of python-lorem
is used to generate Lorem ipsum words.
Example 1: Generate a text containing 3 words
>>> import lorem >>> lorem.get_word(count=3)
Output:
laboris tempor ea
Example 2: Generate a text containing 3 capitalized words
>>> import lorem >>> lorem.get_word(count=3, func='capitalize')
Output:
Laborum Amet Duis
Example 3: Generate a text containing 3 words in all capital letters
>>> import lorem >>> lorem.get_word(count=3, func=lambda s: s.upper())
Output:
EXERCITATION ADIPISCING UT
Function Declaration
get_word(count=1, sep=' ', func=None, args=[], kwargs={}) -> str
Parameter Description
count
: The number of words to generate, the default value is 1.sep
: Separator between words, default is space.func
: Filter function, used to do some processing on the output words, such as capitalizing the first letter, the default is no filter function. If you specify the func function, each word to be returned will be passed to the function for processing, and the processed word will be returned to the caller.args
: Positional arguments passed to func.kwargs
: Keyword arguments passed to func .
4. Generate Sentences
The get_sentence
function of python-lorem
is used to generate Lorem ipsum sentences.
Example 1: Generate a text containing 1 sentence
>>> import lorem >>> lorem.get_sentence()
Output:
Cupidatat officia mollit in, laborum cillum amet pariatur.
Example 2: Generate a text containing 2 sentences, and the number of words in each sentence is a random number between 1 and 3
>>> import lorem >>> lorem.get_sentence(count=2, word_range=(1, 3))
Output:
Commodo nisi et, officia voluptate. Id do.
Function Declaration
get_sentence(count=1, comma=(0, 2), word_range=(4, 8), sep=' ') -> Union[str]
Parameter Description
count
: The number of sentences to generate, the default value is 1.comma
: The number of commas in each sentence, the default value is a random number between 0 and 2.word_range
: The number of words contained in the comma-separated clauses in each sentence, the default value is a random number between 4 and 8.sep
: Separator between sentences, default is space.
5. Generate Paragraph
The get_paragraph
function of python-lorem
is used to generate Lorem ipsum paragraphs.
Example 1: Generate a text containing a paragraph
>>> import lorem >>> lorem.get_paragraph()
Output:
Sed aliqua amet eu ad quis. Minim voluptate velit velit officia non sint lorem. Ea labore nostrud nisi, duis ea cupidatat consequat ipsum cillum. Adipiscing dolor aliqua exercitation sunt exercitation ad. Id et esse exercitation velit, eiusmod ullamco esse proident, sit nulla et sed tempor. Ex lorem aute qui. Laboris irure deserunt dolor, consequat fugiat duis sunt, proident laborum nisi amet.
Example 2: Generate a text containing 2 paragraphs, each paragraph containing 1~2 sentences
>>> import lorem >>> lorem.get_paragraph(count=2, sentence_range=(1, 2))
Output:
Nulla nostrud ut pharetra sunt nostrud est mollit.\r\nNostrud dolor tempor habitant consequat.
Function Declaration
get_paragraph(count=1, comma=(0, 2), word_range=(4, 8), sentence_range=(5, 10), sep=os.linesep)
Parameter Description
count
: The number of paragraphs to generate, the default value is 1.comma
: The number of commas in each sentence, the default value is a random number between 0 and 2.word_range
: The number of words in each sentence, the default value is a random number between 4 and 8.sentence_range
: The number of sentences in each paragraph, the default value is a random number between 5 and 10.sep
: The delimiter between paragraphs, the default value is the delimiter of the current operating system, which is\r\n
under Windows, and\n
under POSIX systems (various UNIX systems, including Linux).
6. Custom Thesaurus
The method of generating words, sentences, and paragraphs described above relies on a thesaurus in the background. By randomly selecting words from the thesaurus and combining them according to the rules specified by the user, the words, sentences, or paragraphs we need are formed. python-lorem
brings a small Lorem ipsum thesaurus, we can customize a Lorem ipsum thesaurus with a richer vocabulary. The set_pool
function of python-lorem
is used to customize the lexicon.
Example : Lorem ipsum lexicon with richer vocabulary
>>> import lorem >>> text = ('ac', 'ad', 'adipiscing', 'aliqua', 'aliquip', 'amet', 'anim', 'aute', 'cillum', 'commodo', 'congue', 'consectetur', 'consequat', 'culpa', 'cupidatat', 'deserunt', 'do', 'dolor', 'dolore', 'duis', 'ea', 'egestas', 'eiusmod', 'elit', 'enim', 'eros', 'esse', 'est', 'et', 'eu', 'ex', 'excepteur', 'exercitation', 'fames', 'fugiat', 'fusce', 'habitant', 'id', 'imperdiet', 'in', 'incididunt', 'ipsum', 'irure', 'labore', 'laboris', 'laborum', 'lectus', 'libero', 'lorem', 'maecenas', 'magna', 'malesuada', 'massa', 'minim', 'mollit', 'morbi', 'netus', 'nisi', 'non', 'nostrud', 'nulla', 'nunc', 'occaecat', 'officia', 'pariatur', 'pellentesque', 'pharetra', 'porttitor', 'posuere', 'proident', 'proin', 'pulvinar', 'purus', 'qui', 'quis', 'reprehenderit', 'sed', 'senectus', 'sint', 'sit', 'sunt', 'tellus', 'tempor', 'tristique', 'turpis', 'ullamco', 'ultricies', 'urna', 'ut', 'velit', 'veniam', 'viverra', 'voluptate') >>> lorem.set_pool(text) >>> lorem.get_sentence()
Output:
Tempor turpis viverra tempor nunc aliquip duis.
7. Lorem ipsum online generator
Lorem lpsum generator is a free online Lorem ipsum generator developed by XYZCodeWorld, you can specify the number of paragraphs to be generated, and the number of sentences in each paragraph.
8. Conclusion
Python is a Swiss Army Knife that can do many things we can think of, and things we can't think of. python-lorem
meets our needs for generating Lorem ipsum text.