美文网首页
python批量打印excel、word、pdf、txt

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

作者: DreamClouds | 来源:发表于2020-03-23 18:36 被阅读0次

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

相关文章

网友评论

      本文标题:python批量打印excel、word、pdf、txt

      本文链接:https://www.haomeiwen.com/subject/rifgyhtx.html