我有一个 pdf 文件,我想替换 pdf 文件中的一些文本并生成新的 pdf。我怎么能在python中做到这一点?我试过 reportlab ,reportlab 没有任何功能来搜索文本和替换它。我可以使用什么其他模块?
我一直在尝试从可以是英语、波斯语、数字或它们的组合的内容创建 PDF 文件。
波斯语文本存在一些问题,例如:“??? ?? ??? ??????? ???”
?- 文字必须从右到左书写
2-单词中不同位置的字符之间存在差异(意味着字符会根据周围的字符改变形状)
3- 因为句子是从右到左阅读的,所以普通的 textwrap 不能正常工作。
这适用于在 PDF 文件中写入文本reportlab:
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
c = canvas.Canvas("test.pdf")
c.drawString(1 * cm, 29.7 * cm - 1 * cm, "Hello")
c.save()
Run Code Online (Sandbox Code Playgroud)
但是在处理多行文本时,不得不处理x, y每一行的坐标是很不愉快的:
text = "Hello\nThis is a multiline text\nHere we have to handle line height manually\nAnd check that every line uses not more than pagewidth"
c = canvas.Canvas("test.pdf")
for i, line in enumerate(text.splitlines()):
c.drawString(1 * cm, 29.7 * cm - 1 * cm - i * cm, line)
c.save()
Run Code Online (Sandbox Code Playgroud)
有没有更聪明的方法来做到这一点 …
我有一个要在 python 中的画布上显示的表格,我在画布中显示了文本,我正在返回缓冲区以在另一个函数中返回一个新的 FileResponse。我的代码:
def Report(dict):
from reportlab.lib.utils import ImageReader
buffer = io.BytesIO()
p = canvas.Canvas(buffer)
textobject = p.beginText()
textobject.setTextOrigin(200, 680)
textobject.textLine('Title')
p.drawText(textobject)
logo = ImageReader('static/img/logo.png')
p.drawImage(logo, 100, 700,width = 400,height=100,mask = None)
data = [['00', '01', '02', '03', '04'],
['10', '11', '12', '13', '14'],
['20', '21', '22', '23', '24'],
['30', '31', '32', '33', '34']]
f = Table(data)
f.setStyle(TableStyle([('BACKGROUND', (1, 1), (-2, -2),
colors.green),
('TEXTCOLOR', (0, 0), (1, -1), colors.red)]))
p.showPage()
p.save()
buffer.seek(0)
return buffer
Run Code Online (Sandbox Code Playgroud) 我正在尝试pip install reportlab==3.0,但我没有运气通过这个错误。有人可以告诉我我缺少什么吗?最新版本的 reportlab 安装正常,但我不能使用它。
我已经尝试过 easy_install 和所有版本。似乎 < 3.0 我会得到同样的错误,但对于 > 3.0 我没问题。
我还确保我的标题已链接。看起来最后甚至还有一些 javascript 错误?这让我很难过
#Attempting install of _rl_accel & pyHnj
#extensions from '/private/var/folders/x6/wyq9wg250c7d7s933sp1fgtw0000gn/T/pip-build-L1Xdbd/reportlab/src/rl_addons/rl_accel'
################################################
################################################
#Attempting install of _renderPM
#extensions from '/private/var/folders/x6/wyq9wg250c7d7s933sp1fgtw0000gn/T/pip-build-L1Xdbd/reportlab/src/rl_addons/renderPM'
will use package libart 2.3.12
# installing without freetype no ttf, sorry!
# You need to install a static library version of the freetype2 software
# If you need truetype support in renderPM
# You may need to edit setup.cfg (win32)
# or …Run Code Online (Sandbox Code Playgroud) 我正在使用reportlab 生成pdf 文件。当我在pdf上绘制字符串时遇到一些问题。如何使用 TTFont 获取字符串的高度?
代码:
# Register fonts.
pdfmetrics.registerFont(ttfonts.TTFont('fz1', 'fz1.ttf'))
pdfmetrics.registerFont(ttfonts.TTFont('fz3', 'fz3.ttf'))
pdfmetrics.registerFont(ttfonts.TTFont('fz4', 'fz4.ttf'))
pdfmetrics.registerFont(ttfonts.TTFont('fz5', 'fz5.ttf'))
pdfmetrics.registerFont(ttfonts.TTFont('w5', 'w5.ttc'))
def draw_text(canvas, fontName, fontSize, x, y, text, cmyk_color=None):
t = canvas.beginText(x * mm, y * mm)
t.setFont(fontName, fontSize)
if cmyk_color is None:
cmyk_color = (0, 0, 0, COLOR_DIV_RATIO)
canvas.setFillColorCMYK(cmyk_color[0] / COLOR_DIV_RATIO,
cmyk_color[1] / COLOR_DIV_RATIO,
cmyk_color[2] / COLOR_DIV_RATIO,
cmyk_color[3] / COLOR_DIV_RATIO)
t.textLine(text)
canvas.drawText(t)
c.drawImage('f1.jpg', 0, 0, CANVAS_WIDTH * mm, CANVAS_HEIGHT * mm)
draw_text(c, 'fz1', 15, mm2pixel(5), mm2pixel(45), u'This is a string')
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方式获取字符串的宽度: …
我正在使用 python ReportLab canvas 生成带有水印的覆盖文档,以将其合并到源 pdf 文档中(使用PyPDF2)。最近,我遇到了包含旋转页面的文档的问题(pdf 中的页面对象存在 /Rotate 键)。该文档在设备和打印机上看起来没问题。但结果(合并)文档包含针对源文档旋转的水印。
所以源页面的 pdf 结构如下:
6 0 obj
<</Length 45>>
stream
q
1 0 0 1 2 4 cm
799 0 0 603 0 0 cm
/x5 Do
Q
endstream
endobj
7 0 obj
<</Type/Page/Parent 1 0 R
/Resources << /XObject << /x5 5 0 R >> >>
/MediaBox [0 0 792 612]
/Rotate 270/Contents 6 0 R
>>
endobj
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,页面旋转了 270 度。
我使用类似的脚本来生成和合并水印和源页面:
6 0 obj
<</Length 45>>
stream …Run Code Online (Sandbox Code Playgroud) 我的桌子太长,超过一页。
我知道使用 doc.build 很容易解决这个问题,但是我正在使用 canvas.save 并且想知道是否可以将表格拆分到多个页面而不将我的所有代码切换到 doc.build。
下面是一些示例代码:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle
from reportlab.lib.colors import pink, green, brown, white, black, gray
from reportlab.lib.units import inch
canvas = canvas.Canvas("CoverSheet.pdf", pagesize=letter)
data = ['1','2','3','4','5','6','7','8','9']
table = Table(data)
table.setStyle(TableStyle([
('INNERGRID', (0,0), (-1,-1), 0.25, black),
('BOX', (0,0), (-1,-1), 0.25, black),
]))
w, h = table.wrapOn(canvas, inch * 1, inch * 4)
table.drawOn(canvas, inch * 1, inch * 1 - h)
canvas.save() …Run Code Online (Sandbox Code Playgroud) 在 ReportLab 中,我有一个由 2 个垂直框架组成的页面模板。我在这里想要实现的是 - 将一些动态文本放入页面(第一帧)后,我想转到第二帧的顶部。
我尝试通过计算第一帧中文本对象的高度,然后插入高度等于(doc.height - 第一帧中文本对象的重量)的间隔符来实现此目的。然而,这是行不通的。这是简化的代码及其输出。
from reportlab.lib.pagesizes import A4, landscape
from reportlab.lib.units import inch
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import *
if __name__ == "__main__":
style_1 = ParagraphStyle(name='Stylo',
fontName='Helvetica',
fontSize=20,
leading=12)
doc = BaseDocTemplate('test_spacer.pdf', showBoundary=1,
pagesize=landscape(A4), topMargin=30,
bottomMargin=30,
leftMargin=30, rightMargin=30)
frameCount = 2
frameWidth = (doc.width) / frameCount
frameHeight = doc.height - .05 * inch
frames = []
column = Frame(doc.leftMargin, doc.bottomMargin, 200, doc.height - .05* inch)
frames.append(column)
column = Frame(doc.leftMargin + 200, doc.bottomMargin, …Run Code Online (Sandbox Code Playgroud) 首先,我注意到有很多相关的问题,但是在尝试了 pyvip 和 cairo 以及其余的一天之后,即使在安装了它们似乎依赖的其他软件之后,它们都对我不起作用。例外是 svglib 和 reportlab,它很接近,但还没有完全达到目标!这是我发现的最好的帖子,可能会对一些人有所帮助。
我的所有源图像都保存在 SVG 文件中。大多数应用商店要求您提供一组具有特定尺寸和质量的 PNG。所以我需要获取一个 SVG 并生成一个宽度为 w、高度为 h 以及特定 dpi 的 PNG。我想在 python 中以编程方式执行此操作。
我编写了一个几乎可以工作的函数,但是缩放和 dpi 以奇怪的方式相互作用。我使用 svglib 将 SVG 转换为 ReportLab 绘图,然后使用 reportlab 操作该绘图。与其他一些选项不同,Windows 上的安装过程很顺利。
pip install svglib
pip install reportlab
Run Code Online (Sandbox Code Playgroud)
代码如下。我检查了上面的库来获取参数,但添加了一些东西来获取特定的大小。
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
def svg_to_png(in_path,out_path,fmt="PNG",
scale=None,size=None,scale_x=1,size_x=None,scale_y=1,size_y=None,
dpi=72, bg=0xffffff):
# Convert SVG to ReportLab drawing.
drawing = svg2rlg(in_path)
# Work out scale factors
# Scale over-rides scale_x|y, ditto size
scale_x = scale if …Run Code Online (Sandbox Code Playgroud)