python生成PDF文件(画线/插图/写文字)

        安装模块

pip install reportlab

        代码示例

from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont


c = canvas.Canvas('xxxx.pdf', pagesize=(283, 283))  # 创建xxxx.pdf,大小为283px×283px
# 导入字体,字体文件在windows系统的存放路径为:C:\Windows\Fonts
pdfmetrics.registerFont(TTFont('SimSun', 'simsun.ttc'))  # 宋体
pdfmetrics.registerFont(TTFont('SimSunBold', '宋体-粗体.ttf'))  # 宋体-粗体

c.setFont("SimSun", 12)  # 设置字体为12磅宋体

"""
绘制一个表格,实际由多条线组合而成,宽273,高273
左边有8行,列宽85
右边有7行,列宽188
"""
c.line(5, 278, 278, 278)  # 画上边框
c.line(5, 5, 278, 5)  # 画下边框
c.line(5, 278, 5, 5)  # 画左边框
c.line(278, 278, 278, 5)  # 画右边框
c.setStrokeColorRGB(127/255, 127/255, 127/255)  # 设置线的颜色为灰色(127, 127, 127)
c.line(90, 278, 90, 5)  # 画中间竖线
c.setStrokeColorRGB(0, 0, 0)  # 设置线的颜色为黑色(0, 0, 0)
for line in range(6):
    c.line(5, 253 - line * 25, 278, 253 - line * 25)  # 画表格内横线
c.line(5, 103, 90, 103)  # 画第7横

# 在左边列写入文字内容
texts = ['标题1', '标题2', '标题3', '标题4', '标题5', '标题6', '备注']
for line in range(len(texts)):
    text_width = c.stringWidth(texts[line])  # 获取在当前字体下这串文字的宽度
    x_center = (85 - text_width) / 2
    y_position = 278 - (line + 1) * 25 + 7.5
    c.drawString(x_center + 5, y_position, texts[line])  # 写文字,并使文字居中对齐

# 在右边列写入内容
texts = ['内容1', '内容2', '内容3', '内容4', '内容5', '内容6']
for line in range(len(texts)):
    if line <= 1:
        c.setFont("SimSunBold", 14)  # 设置字体为14磅宋体-粗体
        text_width = c.stringWidth(texts[line])
        x_center = (188 - text_width) / 2
        y_position = 278 - (line + 1) * 25 + 6.5
        c.drawString(x_center + 90, y_position, texts[line])
    else:
        c.setFont("SimSun", 12)  # 设置字体为12磅宋体
        text_width = c.stringWidth(texts[line])
        x_center = (188 - text_width) / 2
        y_position = 278 - (line + 1) * 25 + 7.5
        c.drawString(x_center + 90, y_position, texts[line])

# 写入备注
remark = '这是一个备注1!这是一个备注2!'
sections = []
index = 0
# 因为左边列宽只有85px,要计算备注的宽度并分行
for i in range(len(remark) + 1):
    text_width = c.stringWidth(remark[index: i])
    if text_width >= 85:
        sections.append([index, i - 1])
        index = i - 1
else:
    sections.append([index, len(remark)])
texts = [remark[i[0]: i[1]] for i in sections]
for text in texts:
    y_position = 54 + 6 * len(texts) - 12 * texts.index(text) - 12
    c.setFont("SimSun", 12)
    c.drawString(5, y_position, text)  # 水平方向左对齐垂直方向居中对齐写入备注文字

# 在表格右边列最下一格插入图片xxxx.png,设置图片宽高为116px
c.drawImage('xxxx.png', x=126, y=8, width=116, height=116)

# 保存PDF文件
c.save()

        效果展示

### 提高 Python Matplotlib 子图导出分辨率和清晰度的方法 为了确保生成的子图具有较高的分辨率和清晰度,在使用 `matplotlib` 进行绘图并保存时,可以通过调整多个参数来实现这一目标。 #### 设置图像尺寸和分辨率 通过指定图像的宽度、高度以及每英寸点数(DPI),可以有效提升最终输出的质量。具体来说: - **设置图像大小**:利用 `plt.figure(figsize=(width, height))` 来定义画布的具体尺寸。 - **增加 DPI 值**:在调用 `savefig()` 方法时传入更高的 DPI 参数值,例如 `dpi=300` 或更高[^1]。 ```python import matplotlib.pyplot as plt # 创建一个新的图形对象,并设定其大小为8*6英寸 plt.figure(figsize=(8, 6)) # 添加一些数据绘制线形图作为例子 plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) # 保存当前图形至文件,同时指定了分辨率为300 dpi plt.savefig('high_quality_figure.png', dpi=300) # 显示所创建的图表 plt.show() ``` #### 更改保存格式以保持质量 除了 PNG 文件外,还可以考虑其他支持矢量化存储方式的格式如 SVG 和 EPS,这些格式能够更好地保留细节并且不会因为缩放而损失品质[^4]。 对于需要插入到学术文章中的插图而言,EPS 是一种非常理想的选择;而对于 PowerPoint 演示文稿,则推荐使用 PDF 或者 SVG 格式以便于编辑且不失真显示。 #### 配置额外属性优化视觉效果 适当调节线条粗细 (`linewidth`) 及字体大小等样式选项也能进一步改善整体呈现的效果[^2]。 ```python import numpy as np import matplotlib.pyplot as plt x = np.linspace(-np.pi, np.pi, 256) y = np.sin(x) plt.figure(figsize=(8, 6)) plt.plot(x, y, linewidth=2.0) # 加粗线条使更易读取 plt.title("Sine Wave", fontsize=16) # 放大标题文字便于阅读 plt.xlabel("X Axis Label", fontsize=14) plt.ylabel("Y Axis Label", fontsize=14) plt.grid(True) # 启用网格辅助查看坐标位置 plt.tight_layout() # 自动调整布局防止标签被裁剪掉 plt.savefig('optimized_sine_wave.pdf') # 默认情况下会采用较高dpi plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值