python打印资料
python打印文件
用python写了一个遍历目录并打印目录下所有文件的脚本,运行的时候,脚本执行完了,但是没有看到打印机打印文件是怎么回事?
麻烦各位大侠看看什么问题,或者有什么更好的方法添加打印作业。
------解决方案--------------------
貌似你只是开始一个打印任务,但还没传递数据就又关闭,看看下面链接范例...
http://timgolden.me.uk/python/win32_how_do_i/print.html
用python写了一个遍历目录并打印目录下所有文件的脚本,运行的时候,脚本执行完了,但是没有看到打印机打印文件是怎么回事?
麻烦各位大侠看看什么问题,或者有什么更好的方法添加打印作业。
# -*- coding: gbk -*-
import os
import shutil
import win32print
import win32api
fileList = []
sorDir = os.getcwd()
dstDir = sorDir + '.bak'
compressFileFormat = ['rar','zip','7z','ace','arj','bz2','cab','gz','iso','jar','lzh','tar','uue','z']
WinRARPath = 'D:\\Programs\\winRAR\\WinRAR.exe'
newFileList = []
myPrinter = ''
def getFileList(fileList,sorDir):
for root,dirs,files in os.walk(sorDir,topdown=True):
for file in files:
fileList.append(os.path.join(root,file))
def mkdir(dstDir):
try:
os.mkdir(dstDir)
except OSError:
pass
def copyFile(file,dstDir):
try:
shutil.copy(file,dstDir)
except OSError:
pass
def isCompressFile(file):
if (os.path.splitext(file)[1][1:].lower() in compressFileFormat)== True:
return True
else:
return False
def deCompressFile(file,dstDir):
cmd =WinRARPath + ' x -ibck ' + file + ' * ' + dstDir + '\\'
os.system(cmd)
def getWinRARPath():
global WinRARPath
print "请输入WinRAR的安装路径,例如\nD:\\Programs\\winRAR\\WinRAR.exe"
WinRARPath = raw_input()
def initPrinter():
global myPrinter
myPrinter = win32print.GetDefaultPrinter()
print "当前默认打印机:" + myPrinter
print "是否设置其他打印机为默认打印机?(Y/N)"
res = raw_input()
if res.upper() == 'Y':
myPrinter = res
win32print.SetDefaultPrinter(myPrinter)
def printFile(file,myPrinter):
PH = win32print.OpenPrinter(myPrinter)
print '开始打印文档:' + file
res = win32print.StartDocPrinter(PH,1,(file,None,'RAW'))
print '打印文档' + file + '完成'
PH.close()
if __name__ == '__main__':
mkdir(dstDir)
getFileList(fileList,sorDir)
getWinRARPath()
for file in fileList:
if isCompressFile(file):
deCompressFile(file,dstDir)
else:
copyFile(file,dstDir)
getFileList(newFileList,dstDir)
initPrinter()
for newfile in newFileList:
printFile(newfile,myPrinter)
# win32api.ShellExecute(0,'print',newfile,'','',0)
------解决方案--------------------
貌似你只是开始一个打印任务,但还没传递数据就又关闭,看看下面链接范例...
http://timgolden.me.uk/python/win32_how_do_i/print.html