Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Apr 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Create PDF files from Excel tab sheets using Python

    Hi,
    I'm trying to convert an excel file( Filename is Input.xlsx) to PDF. This excel file has three sheets within(Sheet1, Sheet2, Sheet3). Each excel sheet needs to be converted into separate pdf with sheet name and these files should be stored inside the (month_year) folder. I have successfully achieved converting the excel into pdf file. When I convert the the file from excel to Pdf, a file is auto generated(First image attached [1]) and within this file, the three sheets are converted as PDF(Second image attached [2]). I do want the pdf files inside this auto generated file, instead I want the three separate pdf files inside this folder. Also, the names are getting generated as input1,input2,input3; but I need the pdf files names as Sheet1, Sheet2, Sheet3. Could anyone please let me know how can i achieve this?


    Code:
        #import the xlrd module
        import xlrd
        import os
        import os.path
        #import client
        import win32com.client
        from win32com import client
        import datetime
        year=datetime.date.today().strftime("%Y")
        print("Current year:",year)
    
        month=datetime.date.today().strftime('%B')
        print('Month is',month)
        monthyear=month+year
        print('Month_year  is',monthyear)
        workdir="C:\\Users\\tj\\Desktop"
        file_location= os.path.join(workdir, 'input_'+monthyear+'.xlsx')
        print ("The names of combined   file_location is",file_location)
        workbook=xlrd.open_workbook(file_location)
    
        #open a sheet to import data 
        sheet=workbook.sheet_by_index(0)
        sheet.Visible=1
    
        # Now to print the number of worksheets in the excel file 
        print ("The number of worksheets are ", workbook.nsheets)
    
        # Now the names of the worksheets in excel file 
        print ("The names of worksheets are", workbook.sheet_names())
        out_folder = workdir + "\\excel_PDF"
        Pdf_folder=out_folder+"\\"+monthyear
    
    
        os.chdir(workdir)
        print ("The names of out_folder is",out_folder )
        if not os.path.exists(out_folder):
        print ('Creating output folder...')
        os.makedirs(out_folder)
        print (out_folder, 'created.')
        else:
            print (out_folder, 'already exists.\n')
    
        print ("The names of pdf_folder is",Pdf_folder ) # returns an array of names
    
        if not os.path.exists(Pdf_folder):
        print ('Creating pdf folder...')
        os.makedirs(Pdf_folder)
        print (Pdf_folder, 'created.')
        else:
        print (Pdf_folder, 'already exists.\n')
              file_type = 'xlsx'
            sheet_name=workbook.sheet_names()
        files='input.xlsx'
        excel = client.DispatchEx("Excel.Application")
        for filess in os.listdir("."):
        if filess.endswith(".xlsx") or filess.endswith('xls'):
          out_name = filess.replace(file_type, r"pdf")
          in_file = os.path.abspath(workdir + "\\" + 'input_'+monthyear+'.xlsx')
          print("in_file is", in_file)
          out_file = os.path.abspath(Pdf_folder +"\\"+out_name)
          print("out_file is", out_file)
          docu=excel.Workbooks.Open(in_file)
          print("Exporting", in_file)
          docu.SaveAs(out_file, FileFormat=56)
          docu.Close()



    [1]: -auto-png
    [2]: -sheet-png
    Last edited by vinyl-junkie; Apr 24th, 2018 at 04:21 PM. Reason: added code tags


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •