Example #1
0
                  indent='15px',
                  align='justify')
document.add_text(
    'another text')  # defaults: size='16px', indent='0' alight='left'

# Embed images
document.add_header('images section')
for i in range(0, 50):
    # get dummy image
    image_array = np.random.randint(0, 256, size=(4, 4, 3), dtype=np.uint8)
    if i > 47:
        # Enforce new line
        document.add_line_break()
    # Add image
    document.add_image(image=image_array,
                       title=f'image{i}',
                       height=32,
                       pixelated=True)
    # add_image method accepts `image` in multiple forms:
    # - numpy array (as in example above)
    # - PIL Image
    # - image path (str or pathlib.Path)

# Embed pandas DataFrame
document.add_header('table section')
num_rows = 5
num_cols = 10
df = pd.DataFrame(
    data=np.random.randn(num_rows, num_cols),
    index=pd.date_range('19700101', periods=num_rows),
    columns=[f'c{i}' for i in range(num_cols)],
)
Example #2
0
def test_HTMLDocument():
    document = HTMLDocument()

    assert document.style != ''

    document.set_title(title='title')

    document.add_header(header='header', level='h2', align='left')

    document.add_text(text='text', size='15px', indent='0', align='left')

    document.add_line_break()

    document.add_page_break()

    document.add_table(df=_get_df())

    try:
        document.add_table(df=[_get_df()])
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "df is of type <class 'list'>, but it should be of type <class 'pandas.core.frame.DataFrame'>."

    document.add_image(image=_get_image_array(),
                       title='title',
                       height=320,
                       width=480,
                       pixelated=False)

    document.add_image(image=_get_PIL_Image())

    _create_test_image()

    document.add_image(image=_TEST_IMAGE_PATH)

    document.add_image_link(image_link=_TEST_IMAGE_PATH,
                            title='title',
                            width='100%')

    document.add_image(image=pathlib.Path(_TEST_IMAGE_PATH))

    document.add_image_link(image_link=pathlib.Path(_TEST_IMAGE_PATH))

    _remove_test_image()

    try:
        document.add_image(image=_get_image_array().astype(np.float32))
    except Exception as e:
        assert isinstance(e, RuntimeError)
        assert str(e) == 'image.dtype is float32, but it should be uint8.'

    try:
        document.add_image(
            image=np.random.randint(0, 256, size=(1, 200, 200,
                                                  3)).astype(np.uint8))
    except Exception as e:
        assert isinstance(e, RuntimeError)
        assert str(e) == 'image.ndim is 4, but it should be 2 or 3.'

    try:
        document.add_image(image=[_get_image_array()])
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "image is of type <class 'list'>, but it should be one of: <class 'numpy.ndarray'>, <class 'PIL.Image.Image'>, <class 'pathlib.Path'> or <class 'str'>."

    try:
        document.add_image_link(image_link=_get_image_array())
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "image_link is of type <class 'numpy.ndarray'>, but it should be <class 'pathlib.Path'> or <class 'str'>."

    document.write(_TEST_DOCUMENT_PATH)

    assert os.path.exists(_TEST_DOCUMENT_PATH)

    _remove_test_document()