2015-05-19 98 views
2

我創建了一個桌面應用程序,從打印機打印標記,使用python2.7GTK + 3的Windows操作系統。我的應用程序中的按鈕應該從文件調用打印。對於格式化打印,我使用了.rtf文件,打開文件之前打開相應的文本編輯器(在我的情況下是MS Word),然後立即關閉。如何直接打印而不顯示在Windows中使用python腳本的打印對話框?

如何在打印之前避免打開和關閉?無論是使用MS Word設置,Windows還是Python解決方案。

這裏是我的代碼:

def make_print(self): 
    os.startfile("print.rtf", "print") 

注 「print.rtf」 是由在此之前調用一個python腳本創建。

我也試過這個,但它甚至沒有打印。

def make_print1(self): 
    with open('print.rtf', 'r') as f, open('LPT1:', 'w') as lpt: 
     while True: 
      buf = f.read() 
      if not buf: break 
      lpt.write(buf) 
+0

我不知道我是否很好地理解你的問題,但你爲什麼不直接使用'time.sleep( )'? – farhawa

+1

打印是什麼意思?創建一個rtf文件?或者你想讓物理打印機將這些文本放在頁面上? – SuperBiasedMan

+0

@WajdiFarhani:它在調用make_print()方法時正在打印。但問題是,在打印之前,屏幕等對話框會在閃光燈中自動打開並關閉。屏幕可能是MS Word應用程序打印對話框。我的問題是如何不顯示這個開放和關閉該對話框。 –

回答

4

此解決方案僅適用於Windows。爲此,您需要安裝pywin32 [http://timgolden.me.uk/pywin32-docs/contents.html]模塊。

不用創建rtf或ps,我們可以直接使用DC(設備上下文)將它發送到打印機。

這裏是我嘗試過的解決方案。

import win32print, win32ui, win32gui 
import win32con, pywintypes 

# create a dc (Device Context) object (actually a PyCDC) 
dc = win32ui.CreateDC() 

# convert the dc into a "printer dc" 

# get default printer 
printername = win32print.GetDefaultPrinter() 
# leave out the printername to get the default printer automatically 
dc.CreatePrinterDC(printername) 

# you need to set the map mode mainly so you know how 
# to scale your output. I do everything in points, so setting 
# the map mode as "twips" works for me. 
dc.SetMapMode(win32con.MM_TWIPS) # 1440 per inch 

# here's that scaling I mentioned: 
scale_factor = 20 # i.e. 20 twips to the point 

# start the document. the description variable is a string 
# which will appear in the print queue to identify the job. 
dc.StartDoc('Win32print test') 

# to draw anything (other than text) you need a pen. 
# the variables are pen style, pen width and pen color. 
pen = win32ui.CreatePen(0, int(scale_factor), 0) 

# SelectObject is used to apply a pen or font object to a dc. 
dc.SelectObject(pen) 

# how about a font? Lucida Console 10 point. 
# I'm unsure how to tell if this failed. 
font = win32ui.CreateFont({ 
    "name": "Lucida Console", 
    "height": int(scale_factor * 10), 
    "weight": 400, 
}) 

# again with the SelectObject call. 
dc.SelectObject(font) 

# okay, now let's print something. 
# TextOut takes x, y, and text values. 
# the map mode determines whether y increases in an 
# upward or downward direction; in MM_TWIPS mode, it 
# advances up, so negative numbers are required to 
# go down the page. If anyone knows why this is a 
# "good idea" please email me; as far as I'm concerned 
# it's garbage. 
dc.TextOut(scale_factor * 72, 
    -1 * scale_factor * 72, 
    "Testing...") 

# for completeness, I'll draw a line. 
# from x = 1", y = 1" 
dc.MoveTo((scale_factor * 72, scale_factor * -72)) 
# to x = 6", y = 3" 
dc.LineTo((scale_factor * 6 * 72, scale_factor * 3 * -72)) 

# must not forget to tell Windows we're done. 
dc.EndDoc() 

在測試了windows8.1/Python的3.4

參考:http://newcenturycomputers.net/projects/pythonicwindowsprinting.html

+0

謝謝@Ayyappadhas ..這個解決方案適合我。 –

相關問題