Skip to main content

Presentation

This module allows to generate jpeg, pdf, text or html preview for virtually any kind of files. It allows to generate preview for a given page. The context of creation of this module (as an example of use context) was for Tracim, a github project (https://github.com/Tracim/tracim) where users can put file on a repository in order to share it with other users. The only way to find a file was with his name. Hence it was decided to generate previews of the files in order to ease the location of one. Only works on Linux.

It is distributed with MIT license (https://choosealicense.com/licenses/mit/)

Format handled

JPEG

PDF

TEXT

HTML

JSON

PNG

JPEG

BMP

GIF

PDF

Compressed files

Office files (word, LibreOffice)

Text

Installation

pip install PyPreviewGenerator

Requirement

This package uses several libraries :

  • wand

  • python-magick

  • pillow

  • PyPDF2

These should be automatically installed with the pip install PyPreviewGenerator command. But if some error occurs quoting one of these library, try to manually install them with a simple pip install …

WARNING! about LibreOffice

If you want to use the conversion from an office file to pdf or jpeg, ensure that LibreOffice is already installed on the computer because the conversion is made by the Libreoffice’s export feature.

LibreOffice’s download page : https://fr.libreoffice.org/download/libreoffice-stable/

Usage

Getting a preview

from PyPreviewGenerator.manager import PreviewManager
manager = PreviewManager(path='/home/user/Pictures/')
path_to_file = manager.get_jpeg_preview(
  file_path='/home/user/Pictures/myfile.gif',
  height=100,
  width=100,
)
print('Preview created at path : ', path_to_file)

The preview manager

preview_manager = PreviewManager(cache_path)

args :

cache_path : a String of the path to the directory where the cache file will be stored

returns :

a PreviewManager Object

The builders

Here is the way it is meant to be used

For Office types into PDF :

preview_manager = PreviewManager(cache_path)
preview = preview_manager.get_pdf_preview(file_path,page=page_id)

-> Will create a preview from an office file into a pdf file

args :

file_path : the String of the path where is the file you want to get the preview

page : the page you want to get. If not mentioned all the pages will be returned

returns :

a FileIO stream of bytes of the pdf preview

For images(GIF, BMP, PNG, JPEG, PDF) into jpeg :

preview_manager = PreviewManager(cache_path)
preview = preview_manager.get_jpeg_preview(file_path,height=1024,width=526)

-> Will create a preview from an image file into a jpeg file of size 1024 * 526

args :

file_path : the String of the path where is the file you want to get the preview

height : height of the preview in pixels

width : width of the preview in pixels. If not mentioned, width will be the same as height

returns :

a FileIO stream of bytes of the jpeg preview

Other conversions :

The principle is the same as above

Zip to text or html : will build a list of files into texte/html inside the json

Office to jpeg : will build the pdf out of the office file and then build the jpeg.

Text to text : mainly just a copy stored in the cache

Release files for PyPreviewGenerator 0.1.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyPreviewGenerator 0.1.15
File Size Uploaded
PyPreviewGenerator-0.1.15.tar.gz 8.3 kB Details

Release files / PyPreviewGenerator-0.1.15.tar.gz

Download URL PyPreviewGenerator-0.1.15.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
945b61cbd34fac4b445a45463cfda71308c85c612e7e63468346fd05fd2fab59
BLAKE2b-256 checksum
How to use checksums
3ec02ab4bdf3a8d90c5c2b9dda0ebf5a2cb080fda32bda95e24a50edf6acff62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.1.20

1 release file

0.1.19

1 release file

0.1.18

1 release file

0.1.17

1 release file

0.1.16

1 release file

This release

0.1.15 This release

1 release file

0.1.14

1 release file

0.1.13

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page