程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python realizes automatic printing

編輯:Python

This time is really out of practical needs , Want to use python Write a little script
The boss sent me a copy containing hundreds of personal information pdf file , If you open documents one by one , Click Print, I feel like I want to see dazzle .

But I have learned python Of ( Although I haven't finished learning object-oriented ), But I learned to call a third-party library , If there is someone else's code, I can still change it .

Do as you say

Check information , Look at what others have written

It is found that calling a computer program requires win32 This library , And at first glance, I found that this library is very NB, How do you say , This library can interact with computers , For example, a prompt box pops up , Or call some programs built in the system . Too wide a range , And it's not the purpose of learning , Let's talk about it later .

Final , It is found that the two main libraries used are win32api and win32print( It is estimated that this is in charge of the printer ).

install win32, It's just pip install pywin32 Or the following one is installed through the image of Douban
pip install -i http://pypi.douban.com/simple/ pywin32 --trusted-host pypi.douban.com
Other methods are similar to ,pip Installation doesn't say much

Checking data , Basically, the following code will be used for discovery

import win32print
import tempfile
import win32api
def print_file(filename):
open(filename,"r")
win32api.ShellExecute(
0,
"print",
filename,
'/d:"%s"' % win32print.GetDefaultPrinter(),
".",
0
)

We need to pay attention to , In this code filename It should be the file path , Specific to documents

eg, To print 11.pdf file

C:\Users\mik\Desktop\py_word\11.pdf

It's easy next , Write a code to get all the contents of the file , And then call print_file That's it

path = r"C:\Users\mik\Desktop\ Print "
for i in os.listdir(path):
file_path = os.path.join(path,i)
print(" The document you want to print is %s"%i)
print_file(file_path)

ha-ha ,nb, Now it's done , It turned out , every last pdf There are more than ten pages , And I'm going to book them all ....

Next time I may have to write a program to automatically order a document printer

Postscript , Printer selection is the default printer , I use a single-sided printer , If it is double-sided printing , You can modify the settings of the system's default printer , Just find it online ; I think some tutorials need to be installed GSPRINT and Ghostscript, And add it to the system environment , But I can do it without installing ( If there are special requirements, you may need to use “Ghostscript,GhostView”, For example, print specific page numbers , This article There are references ); Of course, it also lies in writing some small scope when looking for the file at the end , For example, just print word And so on. .

if i.endswith(“docx”):
print_file(file_path)

2020.10.23


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved