python批量打印excel、word、pdf、txt

2020-03-23  本文已影响0人  DreamClouds

1.支持批量打印excel、word、pdf、txt文件
2.支持excel多个sheet

图片.png
def startPritn():
    print("start print %s" %path.get())
    i = 0
    file_name = os.listdir(path.get())
    file_dirs = [os.path.join(path.get(), x) for x in file_name]

    while i < len(file_dirs):
        ext = os.path.splitext(file_dirs[i])[1]
        if ext.startswith('.x'):
            # excel
            xlApp = win32com.client.Dispatch('Excel.Application')
            xlApp.Visible = 0
            xlApp.EnableEvents = False
            xlApp.DisplayAlerts = False
            xlBook = xlApp.Workbooks.Open(file_dirs[i])
            xlApp.ActiveWorkbook.Sheets(1).PageSetup.Zoom = False
            xlApp.ActiveWorkbook.Sheets(1).PageSetup.FitToPagesWide = 1
            xlApp.ActiveWorkbook.Sheets(1).PageSetup.FitToPagesTall = 1
            xlBook.PrintOut(1, 99, )
            xlApp.quit()
        else:
            # word pdf txt
            win32api.ShellExecute(
                0,
                "print",
                file_dirs[i],
                '/d:"%s"' % win32print.GetDefaultPrinter(),
                ".",
                0
            )

        print(file_dirs[i])
        time.sleep(1)
        i = i + 1

https://github.com/zmyqucai8/printtool

上一篇 下一篇

猜你喜欢

热点阅读