如何从Python创建PDF文件,包括图像和文本? [英] How do I create a PDF file, including images and text, from Python?

查看:123
本文介绍了如何从Python创建PDF文件,包括图像和文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,可以通过Python程序创建一张PDF文件形式的标签.每个标签都有一个或两个图像,以及几行文本(相同的字体,例如Helvetica或Arial,但大小可能不同,并使用粗体和斜体).这些是标签,确保元素在页面上的正确位置很重要.其中一些标签是地址,因此文本可能会有所不同,并且行长和行数也不同.

I am looking for a way to create a sheet of labels, as a PDF file, from a Python program. Each label has one or two images, and a few lines of text (same font, e.g. Helvetica or Arial, but possibly different sizes, and using bold and italic). These being labels, it is important that the elements are positioned correctly on the page. Some of the labels are addresses, so the text can vary and have different line lengths and number of lines.

我想知道什么时候文本不适合它的可用空间,以便该程序可以在尝试之前自动尝试一些策略(例如,将字体更改为Arial Narrow,减小字体大小).我绝对不希望文本溢出或包装而不会发出通知.

I would like to be able to tell when text did not fit in the space available for it, so that the program could try a few strategies automatically (e.g. change font to Arial Narrow, reduce font size) before bailing out. I definitely don't want the text to overrun or wrap without notice.

我看过pypdf,但是它似乎无法满足我的需要(或者可能我缺少了一些东西).我不介意使用支持我需要的功能的库来以其他格式创建文档,只要可以将其以编程方式转换为PDF.作为最后的选择,我还可以使用可以创建光栅图像的库,因为可以将其轻松转换为PDF,但是我更喜欢包含本机文本而不只是位图的PDF.

I have looked at pypdf, but it doesn't seem to be able to do what I need (or maybe I'm missing something). I don't mind creating a document in a different format using a library that supports the features I need, as long as it can be converted to PDF programmatically. As a last resort, I can also use a library that can create a raster image, because that can be converted trivially to PDF, but I prefer a PDF that contains native text, not just a bitmap of it.

推荐答案

看看reportlab.

Take a look at reportlab.

以下是用户指南: https://www.reportlab.com/docs/reportlab-userguide.pdf

这是一个不错的Tutorail: http://www. blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/

Here is a nice Tutorail: http://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/

这篇关于如何从Python创建PDF文件,包括图像和文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆