python 合并Tableau生成的PDF文件而不先保存到磁盘

g2ieeal7  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(121)

我在使用Tableau server client (TSC)库和python从Tableau Online导出PDF时遇到问题。
我有一个工作簿与8 Jmeter 板,我想创建一个过滤的pdf文件。我通过8 Jmeter 板循环,并适用于customerID过滤器到每个。事情是,我想合并成1 pdf文件的整个工作簿为每个客户,它在这里,我遇到了一些问题。
我使用的代码是:

import PyPDF2
import tableauserverclient as TSC

tableau_token_name = 'TOKENNAME'
tableau_token_value = 'TOKENSECRET'

tableau_site_name = 'SITE'
tableau_server_name = 'SERVERNAME'

##Specify the workbook id that is the base for the pdf
tableau_workbook_id = 'WORKBOOKID'
##Specify the filename start for the pdf
pdf_file_name_prefix = 'PREFIX_FOR_PDF_NAME'

##create the login credentials
tableau_auth = TSC.PersonalAccessTokenAuth(tableau_token_name, tableau_token_value, tableau_site_name)
server = TSC.Server(tableau_server_name, use_server_version=True, http_options={"verify": False})

##Create the export options for the pdf
pdf_req_option = TSC.PDFRequestOptions(page_type=TSC.PDFRequestOptions.PageType.A5,orientation=TSC.PDFRequestOptions.Orientation.Landscape)
    
##create a pdf merger object 
PDFMerger = PyPDF2.PdfMerger()

with server.auth.sign_in(tableau_auth):
    ##Get the workbook that needs exporting
    workbook = server.workbooks.get_by_id(tableau_workbook_id)
    
    ##get the views from the workbook
    server.workbooks.populate_views(workbook)

    ##create a list of views from the workbook to iteratate on
    view_list = [view for view in workbook.views]

    
    customerIDs = ["1","2","3"]

    for customerID in customerIDs:

        for view in view_list:
            
            ##set export filter
            pdf_req_option.vf("customerID", customerID)

            ##server.views.get_by_id(view_id)
            server.views.populate_pdf(view, pdf_req_option)
            

            PDFMerger.append(view.pdf)

    ##writing the file as an example. The actual script will write to blob storage in azure
    PDFMerger.write("workbook" + str(customerID) + ".pdf")

          
    server.auth.sign_out()

我遇到的问题是PDF合并器不能识别我的pdf对象为pdf,它只是说:

AttributeError: 'bytes' object has no attribute 'seek'

如果我将view.pdf保存为它自己的文件,然后从硬盘中循环保存文件,它会工作得很好。有人对如何在不将文件保存为pdf的情况下完成此操作有任何建议吗?我将在无服务器环境中运行此脚本,我不想保存单独的PDF。
我尝试与**PyPDF2.PdfMerger.append()**函数合并,但由于某种原因,我从Tableau获得的pdf对象无法被识别为pdf对象,似乎缺少一些调用seek的内容。

AttributeError: 'bytes' object has no attribute 'seek'
xqnpmsa8

xqnpmsa81#

错误消息指出您传递了一个没有seek属性的bytes对象。
使用io.BytesIO

from io import BytesIO

# ... your code
stream = BytesIO(view.pdf)
PDFMerger.append(stream)
# ... your code

相关问题