How to resolve " AttributeError: 'Error' object has no attribute 'save_to_file' " error?

I am trying to print a calendar to pdf using the API and am stuck on using the "get_sheet_as_pdf". I attached the code below that I used and am getting the titular error, I went into the sheets.py file to see if I was missing something but there doesn't seem to exist a method "save_to_file".

Also, is it possible to ensure the view will be printed in the Calendar View? I'd like to just have that one.


import smartsheet

# smartsheet access token
access_token = 'XXXXXXXXXXX'

smartsheet_client = smartsheet.Smartsheet(access_token)
sheet_id = 'XXXXXXXXXXXXXX'
path = r'C:\path\to\folder'

smartsheet_client.Sheets.get_sheet_as_pdf(
    sheet_id,
    path,
    'A4',
    'test_pdf.pdf'
)

Best Answer

  • Parker Oxford
    Parker Oxford ✭✭✭✭✭
    Answer ✓

    Hi @Genevieve P.


    After contacting support, it seems I am trying to get a report to be printed as a PDF when it only has functionality for "Sheets" as PDFs. There are methods to print reports to excel/csv but nothing for PDFs. I put in a request for printouts to be in Calendar View so we can only hope that they can get around to implementing that feature for sheets/reports/dashboards.


    Thank you for your help on this.

    Parker

Answers

  • Parker Oxford
    Parker Oxford ✭✭✭✭✭

    I see a similar question here but it's recommended solution was covered in the code I've provided.

  • Hi @Parker Oxford

    The error you're receiving means that the API call to Smartsheet is failing; the specific error is being obscured by the next line in the SDK code trying to generate a file from the response data.

    This could either be related to your access token or the sheet ID (for example, if your token isn't valid anymore or you're not shared to this specific sheet). Can you check these two items and then try again? Are you able to export this sheet from the Core App directly?

    Currently the only view supported by the API for exporting to PDF is Grid View. Please let the Product team know of your request by filling out this form, here!

    Thanks,

    Genevieve

    Join us at Smartsheet ENGAGE 2024 🎉
    October 8 - 10, Seattle, WA | Register now

  • Parker Oxford
    Parker Oxford ✭✭✭✭✭

    Hi @Genevieve P. ,


    I have verified my access token to the Smartsheet API by making another call on another sheet. I am also the owner of the sheet in question so I'm unsure if it's the permissions causing the error.

    I am able to manually print out the sheet to a PDF. I have also put in a request from your link to try to get Calendar View as an available option for the API to return in PDF form.


    Any help is much appreciated. Thank you,

    Parker

  • Hi @Parker Oxford

    The only other thing besides a token/permissions issue I can think of is that the sheet_id parameter is meant to be of type int rather than str. However, when reading the code I'm not sure it should matter. You can try changing it to an int, but still the signs point to an issue with token/permissions at this time.

    I would suggest turning on debug logging to see the actual response that's coming back from the Smartsheet API which will tell us exactly what's wrong.

    The help page on enabling/adjusting logging for the python SDK is here: https://github.com/smartsheet-platform/smartsheet-python-sdk/blob/master/ADVANCED.md#logging

    After this, I would suggest reaching out directly to Smartsheet Support so we can continue troubleshooting with you in a more private channel where you can share full calls/responses, et.

    Cheers!

    Genevieve

    Join us at Smartsheet ENGAGE 2024 🎉
    October 8 - 10, Seattle, WA | Register now

  • Parker Oxford
    Parker Oxford ✭✭✭✭✭
    edited 11/04/21

    Hi @Genevieve P.


    It throws an error below on the "smartsheet_client.Sheets.get_sheet_as_pdf(" line in the "sheets.py" file from the SDK.

    Attempting to resolve this with the "int" parameter seems to return the same error so it seems to not get that far. I assume the error originates from an issue within the sheets.py file that it's trying to reference in the command. It seems this is a cause for other issues in similar functions (get_sheet_as_csv etc.)

    {"response": {"statusCode": 404, "reason": "Not Found", "content": {"errorCode": 1006, "message": "Not Found", "refId": "7i7zvofrhpdj"}}}
    Traceback (most recent call last):
      File "C:\Users\poxford\PycharmProjects\pythonProject\pto_calendar2.py", line 18, in <module>
        smartsheet_client.Sheets.get_sheet_as_pdf(
      File "C:\Users\poxford\PycharmProjects\pythonProject\venv\lib\site-packages\smartsheet\sheets.py", line 629, in get_sheet_as_pdf
        response.save_to_file()
    AttributeError: 'Error' object has no attribute 'save_to_file'
    
  • Hi @Parker Oxford

    The 1006 error is saying that the Sheet is not found. I've been able to replicate this same error if I take out one character from my sheet's ID. Have you double checked that the Sheet ID is exactly the same as what's in Smartsheet? Is it possible there's an extra character there blocking the API call?

    Are you able to successfully make this call with a different Sheet ID?

    At this point, if none of this has helped, please contact Support so that you can provide them with the Sheet URL and screen captures including sensitive information such as your sheet ID, etc.

    Thanks!

    Genevieve

    Join us at Smartsheet ENGAGE 2024 🎉
    October 8 - 10, Seattle, WA | Register now

  • Parker Oxford
    Parker Oxford ✭✭✭✭✭

    Hi @Genevieve P.


    Yes running the script on a separate sheet ran with no error. Any idea what's causing the issue with the other error? I ensured every single digit is correct and even copied it from the "Properties" section of the sheet as well as tried other sheets with abnormal typing. Are certain types of sheets not allowed to be printed as pdf through the API? I tried reading the documentation and didn't see anything on it.

    Thank you for your help again,

    Parker

  • Hi @Parker Oxford

    If it's a Sheet type of item and you can export it as expected via the UI then you should be able to do the same export to PDF action in the API.

    At this point I am unable to help further without seeing more details, so troubleshooting directly with Support will be a better avenue to continue working on this error. Once you have a resolution with them I would appreciate hearing an update!

    Thanks,

    Genevieve

    Join us at Smartsheet ENGAGE 2024 🎉
    October 8 - 10, Seattle, WA | Register now

  • Parker Oxford
    Parker Oxford ✭✭✭✭✭
    Answer ✓

    Hi @Genevieve P.


    After contacting support, it seems I am trying to get a report to be printed as a PDF when it only has functionality for "Sheets" as PDFs. There are methods to print reports to excel/csv but nothing for PDFs. I put in a request for printouts to be in Calendar View so we can only hope that they can get around to implementing that feature for sheets/reports/dashboards.


    Thank you for your help on this.

    Parker

  • Hi @Parker Oxford

    Thank you for following up and clarifying the item was a Report and not a Sheet! Thank you also for submitting your feature request to the Product team.

    Cheers,

    Genevieve

    Join us at Smartsheet ENGAGE 2024 🎉
    October 8 - 10, Seattle, WA | Register now

  • Make sure the smartsheet token you are using to access the sheet has shared access to the space you are trying to download from. If it's not shared with the token user, you will get this "not found" error.