Hari
Hari

Reputation: 11

Is there a way to convert all the worksheets within a google sheets spreadsheet to individual PDFs

Currently I have a google sheets spreadsheet for a project that I am working on that has a many worksheets. I am looking for a way to convert each worksheet into a PDF where the name of the PDF is the name of the worksheet. I also need the format to be preserved. My current method is to convert each individual sheet into a pdf using the File->Download->.pdf method.

I have tried solutions on appscript and python.

This was the appscript code that I tried

function downloadasPDF() {
    // Get start time of execution
    var startTime = new Date();

    // Get user properties
    var scriptProperties = PropertiesService.getScriptProperties();
    var currentSheetIndex = Number(scriptProperties.getProperty('currentSheetIndex')) || 0;
    // Delete existing trigger


    // Defining variables
    var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
    var sheets = spreadSheet.getSheets();
    var mainSheet = spreadSheet.getActiveSheet();

    // Get or create folder
    var parentFolder = DriveApp.getFolderById("MY FOLDER TO SAVE TO"); 

    var options = {
        method: 'GET',
        headers: {
        Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
        },
        muteHttpExceptions: true,
    };
    Logger.log(sheets.length)
    var fileId = spreadSheet.getId();
    // Loop through the sheets
    for (var i=currentSheetIndex ; i < sheets.length; i++) {
        Logger.log(i)
        // Get current time and check if it exceeds 5 mins
        var currentTime = new Date().getTime();
        if (currentTime - startTime >= 4.75 * 60 * 1000) { // If reaching 4 minutes 45 seconds
            ScriptApp.newTrigger('downloadasPDF')
            .timeBased()
            .after(1000) // Start the next execution after 1 second
            .create();
            break;
        }

        var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", fileId);
        var sheetName = sheets[i].getName();


        var url_ext = 'exportFormat=pdf&format=pdf'        
                    + '&size=A4'                                  
                    + '&portrait=true'                                
                    + '&fitp=true&source=labnol'                       
                    + '&sheetnames=false&printtitle=false'             
                    + '&pagenumbers=false&gridlines=false'             
                    + '&gid=';                                         
                        // Export as PDF
        do var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), options); while (response.getResponseCode() != 200);
        var file = response.getBlob();
        file.setName(sheetName + ".pdf");
        parentFolder.createFile(file);
        scriptProperties.setProperty('currentSheetIndex', i+1);

    }
    if (currentSheetIndex >= sheets.length) {
        scriptProperties.deleteProperty('currentSheetIndex');
    }
}

The above code is based off other stack overflow questions that I found. However, even though I set the trigger to start the same function right after the previous one ends, the trigger does not start and the code ends at the 4.75 min mark ( to prevent the timeout of around 5 minutes)

For python, I could not find a way to do it using Gspread, so I had wanted to convert the whole spreadsheet into excel which would then be converted to a PDF using another library.

class GoogleSheetsClient:
    def __init__(self, credentials_path: str):
            self.credentials_path = credentials_path
            self.gc = gspread.service_account(filename=Path(self.credentials_path))
    def get_client(self):
            return self.gc

def export_googlesheeets_as_excel_file(sheets_clients: GoogleSheetsClient, id: str):
    client = sheets_clients.get_client()
    path = os.path.join("pdf_outputs", "googlesheets.xlsx")
    f = open(path, "wb")
    f.write(
        client.export(
            id,
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        )
    )
    f.close()



def export_as_pdf(base_file):
    try:
        excel = win32com.client.Dispatch("Excel.Application")
        excel.Visible = False
        wb = excel.Workbooks.Open(base_file)
        sheets_in_workbook = wb.Worksheets
        pdf_path = "C:/Path/To/PDF/Folder"
        for i in len(sheets_in_workbook):
            ws = wb.Worksheets[i]
            ws.SaveAs(f"{pdf_path}/{i}.pdf", FileFormat=57)
    except Exception as e:
        print(e)
    finally:
        wb.Close()
        excel.Quit()

The google sheets client is a class that I am using for other google sheets project. The main issue that I am facing is that the excel that I have is not activated, so I am guessing I cant save anything as a PDF.

Would like to know if there are any better ways or libraries to do this task.

Upvotes: 0

Views: 72

Answers (0)

Related Questions