python - Python - 如何将两个 pdf 页面合并为一个页面
问题描述
我正在使用 matplotlib 生成一系列图。每个图都针对不同的位置,因此图中的数据会发生变化。
我还有一个 pdf 模板,我想用它来粘贴其中的图。这个模板比我想在 matplotlib 中复制的要复杂。
我已将透明度设置为 True,因此我希望它能够很好地合并而不会覆盖模板的某些部分。
我尝试的是首先将 matplotlib 图导出为 pdf,然后使用 PyPDF2 将两个 pdf 合并在一起,但这似乎不起作用,因为我只是以两个单独的页面结束,这 duh ......但我我不知道从这里去哪里。
plt.savefig("testhole.pdf", transparent=True)
#Merge pdf's
path = r'C:\Users\xxx\PycharmProjects\xxx'
template_path = path + r'\Template.pdf'
testhole_file = path + r'\testhole.pdf'
test_hole_path = path + r'\\' + location + r'.pdf'
pdf_template = PdfFileReader(template_path)
pdf_testhole = PdfFileReader(testhole_file)
pdf_merger = PdfFileMerger()
pdf_merger.append(pdf_template)
pdf_merger.append(pdf_testhole)
with open(test_hole_path, 'wb') as fileobj:
pdf_merger.write(fileobj)
解决方案
因为 append 需要fileobj
通过 a 我认为你必须使用:
pdf_template = PdfFileReader(open(template_path, 'rb'))
pdf_testhole = PdfFileReader(open(testhole_file, 'rb'))
请让我知道这是否解决了您的问题
推荐阅读
- salesforce - Salesforce Sandbox 播种工具是否通过一些点击将(目标)数据传播到较低的环境真的很有帮助
- reactjs - 升级到 4.7.0 后的 Kendo React Dropdowns 问题
- sql - 我应该在外键上为选择创建索引吗?
- regex - 为什么我的正则表达式显示为一个空字符串?
- flutter - Flutter:在类中保存变量以在不同的小部件中显示它
- javascript - 如何设置 Stripes 元素 CSS 以匹配引导表单控制输入 CSS 属性的大小、垂直对齐、焦点等
- excel - Excel VBA - 如果包含单元格范围内的任何值,则用于过滤列的宏
- javascript - 在 Ember 应用程序中提供来自 S3 的图像资产
- unit-testing - mockito 3,什么是可空的 nayMap() 的替代品?
- javascript - Javascript 数组无法填充循环,但可以正常工作。为什么?