Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make single pdf file from Vitalsource ebook using Python script

Tags:

python

I'm getting an error when I run my python3 script that is supposed to take screenshots of Vitalsource ebook pages and then convert them into a pdf file.

I have tried the suggested solution (by @Balderman) below, but it's not working. However, please note i'm a newbie and learning python. I might have done something. Kindly advise.

My python script (app2.py) code:

import argparse
import os
import tempfile

import autopy
import img2pdf


def screenshot(top_left, right_bottom, next_page, total_page):
    rect_size = (right_bottom[0] - top_left[0], right_bottom[1] - top_left[1])
    images = []
    temp_dir = tempfile.mkdtemp()
    for i in range(total_page):
        page_num = "{}".format(i).zfill(len(str(total_page)))
        file_name = os.path.join(temp_dir, 'book-page-{}.png'.format(page_num))
        images.append(file_name)

        autopy.mouse.move(*next_page)
        autopy.mouse.click(delay=1)
        autopy.bitmap.capture_screen((top_left, rect_size)).save(file_name)

    return images


def image2pdf(images):
    with open("book.pdf", "wb") as f:
        f.write(img2pdf.convert(images))


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Take book screenshots.')
    parser.add_argument('top_left', type=str)
    parser.add_argument('right_bottom', type=str)
    parser.add_argument('next_button', type=str)
    parser.add_argument('total_page', type=int)

    args = parser.parse_args()

    top_left = tuple(map(lambda x: int(x), args.top_left.split(',')))
    right_bottom = tuple(map(lambda x: int(x), args.right_bottom.split(',')))
    next_button = tuple(map(lambda x: int(x), args.next_button.split(',')))
    total_page = args.total_page

    print("Take book screenshot at {} {} and next at {} with {} pages".format(
        top_left, right_bottom, next_button, total_page
    ))

    images = screenshot(top_left, right_bottom, next_button, total_page)
    image2pdf(images)

    print("Done, book saved in book.pdf.")

Suggested Solution by @Balderman:

python code.py 12,34 34,67 12,99 12

import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Take screenshots.')
    parser.add_argument('top_left', type=str)
    parser.add_argument('right_bottom', type=str)
    parser.add_argument('next_button', type=str)
    parser.add_argument('total_page', type=int)

    args = parser.parse_args()
    top_left = tuple(map(lambda x: int(x), args.top_left.split(',')))
    right_bottom = tuple(map(lambda x: int(x), args.right_bottom.split(',')))
    next_button = tuple(map(lambda x: int(x), args.next_button.split(',')))
    total_page = args.total_page

    print("Take screenshot at {} {} and next at {} with {} pages".format(
        top_left, right_bottom, next_button, total_page
    ))

    print(args)

Expected results: print out an entire e-book (1200 pages) in/to pdf format from Vitalsourcesafe which limits printing to 2 pages at a time.

Error message i'm getting:

usage: app2.py [-h] top_left right_bottom next_button total_page
app2.py: error: the following arguments are required: top_left, right_bottom, next_button, total_page
like image 573
patrick.kabuchi Avatar asked Oct 25 '25 00:10

patrick.kabuchi


1 Answers

I've been looking like crazy too, found complicated python scripts to print with PDF printer or do screenshots but it didn't work for me and I was too lazy to debug their scripts.

I've done something much more simpler in Windows 10, I hope it works for you or at least it can inspire you to get your own solution.

Legal disclaimer: Usage of this solution must abide the terms set out by VitalSource at https://support.vitalsource.com/hc/en-us/articles/204612518. In a nutshell this means no commercial usage or distribution, and only personal use is allowed. If you do decide to mass distribute, do so at your own risk.

In the following explanation I assume you have installed Python 3.x and you know you to use pip.

  1. Install ShareX for handling the screenhots (We don't need to reinvent the wheel): https://getsharex.com/

  2. Configure it to have a hotkey to capture last screenshot area with key F3 (any key is valid as long is not occupied by any other application and you adapt the python script): My configuration: Hotkey settigs

  3. Configure your "after capture tasks" My configuration: After capture tasks

  4. Install VitalSource Bookshelf: Install Bookshelf on Windows 10

  5. Open your book with VitalSource Bookshelf and disable all panels you can in View menu to get the image as large as possible. Configure it to display only one page.

  6. Select one page and tap spacebar, it should go to next page by each tap.

  7. Install autopy dependency:

    > pip install autopy
    
  8. Make sure you have your book open and with the first page visible.

  9. Do the first screenshot manually. Try to adjust perfectly the region by using the magnifier ShareX provides. After you are happy with the screenshot delete it (to avoid duplicates).

  10. Run the following python script and quickly change to VitalSource Bookshelf application (Use Alt + Tab):

    import autopy
    
    if __name__ == "__main__":
        while True:
            autopy.key.tap(autopy.key.Code.F3,None,1)      
            autopy.key.tap(autopy.key.Code.SPACE,None,1)
    
  11. Do nothing. You should see how the pages advance automatically.

  12. After a while, once you'll see the last page of your book, stop the script and check the screenshots in your ShareX destination folder.

  13. Delete the duplicated screenshots of the last page if needed.

  14. Select all images and right click Print. Select Microsoft Print to PDF and voilà you have your book in PDF without watermarks!

like image 124
Pau Avatar answered Oct 27 '25 16:10

Pau