Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:18:36+00:00 2026-06-10T23:18:36+00:00

I’m using reportlab to convert some big library (plain text in Russian) into pdf

  • 0

I’m using reportlab to convert some big library (plain text in Russian) into pdf format.
When the original file is small enough (say, about 10-50 kB), it works fine. But if I’m trying to convert big texts (above 500kB) it takes lot of time to reportlab to proceed.
Does anyone knows what could be the problem?

BYTES_TO_READ = 10000 

def go(text):
    doc = SimpleDocTemplate("output.pdf")
    Story = [Spacer(1, 2*inch)]
    style = styles["Normal"]
    p = Paragraph(text, style)
    Story.append(p)
    doc.build(Story)

def get_text_from_file():
    source_file = open("book.txt", "r")
    text = source_file.read(BYTES_TO_READ)
    source_file.close()
    return text

go(get_text_from_file())

So, when I try to set the BYTES_TO_READ variable to more than 200-300 thousands (i.e., just to see what happening, not reading the full book, just some part of it) – it takes HUGE amount of time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:18:37+00:00Added an answer on June 10, 2026 at 11:18 pm

    Let me preface by saying that I don’t have much experience with reportlab at all. This is just a general suggestion. It also does not deal with exactly how you should be parsing and formatting the text you are reading into proper structures. I am just continuing to use the Paragraph class to write text.

    In terms of performance, I think your problem is related to trying to read a huge string once, and passing that huge string as a single paragraph to reportlab. If you think about it, what paragraph is really 500k bytes?

    What you would probably want to do is read in smaller chunks, and build up your document:

    def go_chunked(limit=500000, chunk=4096):
    
        BYTES_TO_READ = chunk
    
        doc = SimpleDocTemplate("output.pdf")
        Story = [Spacer(1, 2*inch)]
        style = styles["Normal"]
    
        written = 0
    
        with open("book.txt", "r") as source_file:
            while written < limit:
                text = source_file.read(BYTES_TO_READ)
                if not text:
                    break
                p = Paragraph(text, style)
                Story.append(p)
                written += BYTES_TO_READ
    
        doc.build(Story)
    

    When processing a total of 500k bytes:

    %timeit go_chunked(limit=500000, chunk=4096)
    1 loops, best of 3: 1.88 s per loop
    
    %timeit go(get_text_from_file())
    1 loops, best of 3: 64.1 s per loop
    

    Again, obviously this is just splitting your text into arbitrary paragraphs being the size of the BYTES_TO_READ value, but its not much different than one huge paragraph. Ultimately, you might want to parse the text you are reading into a buffer, and determine your own paragraphs, or just split on lines if that is the format of your original source:

    def go_lines(limit=500000):
    
        doc = SimpleDocTemplate("output.pdf")
        Story = [Spacer(1, 2*inch)]
        style = styles["Normal"]
    
        written = 0
    
        with open("book.txt", "r") as source_file:
            while written < limit:
                text = source_file.readline()
                if not text:
                    break
                text = text.strip()
                p = Paragraph(text, style)
                Story.append(p)
                written += len(text)
    
        doc.build(Story)
    

    Performance:

    %timeit go_lines()
    1 loops, best of 3: 1.46 s per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.