.xlsx and xls(Latest Versions) to pdf using python

后端 未结 3 1058
一向
一向 2020-11-28 08:37

With the help of this .doc to pdf using python Link I am trying for excel (.xlsx and xls formats)

Following is modified Code for Excel:



        
相关标签:
3条回答
  • 2020-11-28 08:48

    Link of xlsxwriter :

    https://xlsxwriter.readthedocs.org/en/latest/contents.html

    With the help of this you can generate excel file with .xlsx and .xls

    for example excel file generated name is trial.xls

    Now if you want to generate pdf of that excel file then do the following :

    from win32com import client
    xlApp = client.Dispatch("Excel.Application")
    books = xlApp.Workbooks.Open('C:\\excel\\trial.xls')
    ws = books.Worksheets[0]
    ws.Visible = 1
    ws.ExportAsFixedFormat(0, 'C:\\excel\\trial.pdf')
    
    0 讨论(0)
  • 2020-11-28 08:50

    I got the same thing and the same error... ANSWER: 57.... see below...

    from win32com import client
    import win32api
    
    def exceltopdf(doc):
        excel = client.DispatchEx("Excel.Application")
        excel.Visible = 0
    
        wb = excel.Workbooks.Open(doc)
        ws = wb.Worksheets[1]
    
        try:
            wb.SaveAs('c:\\targetfolder\\result.pdf', FileFormat=57)
        except Exception, e:
            print "Failed to convert"
            print str(e)
        finally:
            wb.Close()
            excel.Quit()
    

    ... as an alternative to the fragile ExportAsFixedFormat...

    0 讨论(0)
  • 2020-11-28 08:58

    You can print an excel sheet to pdf on linux using python. Do need to run openoffice as a headless server and use unoconv, takes a bit of configuring but is doable

    You run OO as a (service) daemon and use it for the conversions for xls, xlsx and doc, docx.

    http://dag.wiee.rs/home-made/unoconv/

    0 讨论(0)
提交回复
热议问题