如何将 pdf 文件从 Python 中的临时文件合并为一个文件
how to combine pdf files into one from a tempfile in Python
我有一个 python 代码,可以从画面视图创建临时 pdf 文件并将其单独发送到松弛通道。
我想将它们合并到一个文件中,但我不知道该怎么做。
我是 python 的新手,非常感谢有关如何在下面的代码中使用 PdfFileMerger 的一些帮助。
我试过使用
merger.append(f)
在 f 变量之后但它不起作用给我 ar 错误 ** AttributeError: 'dict' object has no attribute 'seek'
** 我应该把什么放在括号里?
for view_item in all_views :
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=True) as temp_file:
#server.views.populate_image(view_item, req_options=image_req_option)
server.views.populate_pdf(view_item, req_options = pdf_req_option)
print('got the image')
temp_file.write(view_item.pdf)
temp_file.file.seek(0)
print('in the beginnign again')
f = {'file': (temp_file.name,temp_file, 'pdf')}
merger.append(f)
response = requests.post(url='https://slack.com/api/files.upload', data=
{'token': bot_token, 'channels': slack_channels[0], 'media': f,'title': '{} {}'.format(view_item.name, yesterday), 'initial_comment' :''},
headers={'Accept': 'application/json'}, files=f)
print('the image is in the channel')
您需要像这样向 PdfFileMerger 提供文件对象,而不是字典。
由于 PdfFileMerger 无论如何都会在内存中执行操作,因此无需写入磁盘上的临时文件,内存中的 BytesIO 就可以了。
import io
merger = PdfFileMerger()
for view_item in all_views:
server.views.populate_pdf(view_item, req_options=pdf_req_option)
# Write retrieved data into memory file
tf = io.BytesIO()
tf.write(view_item.pdf)
tf.seek(0)
# Add it to the merger
merger.append(tf)
# Write merged data into memory file
temp_file = io.BytesIO()
merger.write(temp_file)
temp_file.seek(0)
f = {'file': ('merged.pdf', temp_file, 'pdf')}
# Slack stuff here...
我有一个 python 代码,可以从画面视图创建临时 pdf 文件并将其单独发送到松弛通道。 我想将它们合并到一个文件中,但我不知道该怎么做。 我是 python 的新手,非常感谢有关如何在下面的代码中使用 PdfFileMerger 的一些帮助。 我试过使用
merger.append(f)
在 f 变量之后但它不起作用给我 ar 错误 ** AttributeError: 'dict' object has no attribute 'seek' ** 我应该把什么放在括号里?
for view_item in all_views :
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=True) as temp_file:
#server.views.populate_image(view_item, req_options=image_req_option)
server.views.populate_pdf(view_item, req_options = pdf_req_option)
print('got the image')
temp_file.write(view_item.pdf)
temp_file.file.seek(0)
print('in the beginnign again')
f = {'file': (temp_file.name,temp_file, 'pdf')}
merger.append(f)
response = requests.post(url='https://slack.com/api/files.upload', data=
{'token': bot_token, 'channels': slack_channels[0], 'media': f,'title': '{} {}'.format(view_item.name, yesterday), 'initial_comment' :''},
headers={'Accept': 'application/json'}, files=f)
print('the image is in the channel')
您需要像这样向 PdfFileMerger 提供文件对象,而不是字典。
由于 PdfFileMerger 无论如何都会在内存中执行操作,因此无需写入磁盘上的临时文件,内存中的 BytesIO 就可以了。
import io
merger = PdfFileMerger()
for view_item in all_views:
server.views.populate_pdf(view_item, req_options=pdf_req_option)
# Write retrieved data into memory file
tf = io.BytesIO()
tf.write(view_item.pdf)
tf.seek(0)
# Add it to the merger
merger.append(tf)
# Write merged data into memory file
temp_file = io.BytesIO()
merger.write(temp_file)
temp_file.seek(0)
f = {'file': ('merged.pdf', temp_file, 'pdf')}
# Slack stuff here...