资源描述:
《用python在windows系统中打印》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、PrintTimGolden>PythonStuff>Win32HowDoI...?>PrintIntroductionTherequirement:toprintThisisprobablythemostwide-rangingquestionI'llhavetoaddresshere,andtheonewiththegreatestdisparitybetweenthenumberandcomplexityofsolutionsandthesimplicityoftherequirement.Theans
2、weris:italldependswhatyou'retryingtoprint,whattoolsyouhaveatyourdisposal,andhowmuchcontrolyouneed.·Ifyousimplyhavea"document"(read:fileofawell-knowntype,associatedwithoneapplication)youwishtoprint,andaren'ttoofussyaboutcontrolling,thenyoucanusetheShellExecu
3、teapproach.Thisworks(assumingyouhavethecorrespondingapplicationsinstalled)forMicrosoftOfficedocuments,PDFfiles,textfiles,andprettymuchanymajorapplication.Tryitandsee.·Thenextmostgeneralcaseiswhereyouhavesomething,forexampleatextfileorrawPCL,whichyouknowyouc
4、ansenddirectlytoaprinter.Inthatcase,youcanusethewin32printfunctionsdirectly.·Ifyouhaveanimagetoprint,youcancombinethepowerofthePythonImagingLibrarywiththewin32uimoduletodoarough-and-readybutusefulprinttoanyprinter.·Ifyouhaveafairamountoftexttoprint,yourbest
5、betistousetheReportlabPDFToolkitanditsPlatypusdocumentsystemtogeneratereadablePDFsfromanyamountoftext,andthenusetheShellExecutetechniquetoprintit.Standarddocument:useShellExecuteMakeuseofthefactthatwithinWin32,filetypes(ineffect,extensions)canbeassociatedwi
6、thapplicationsviacommandverbs.Typicallythesameapplicationwillhandleallverbs(andtypicallythoseverbsareOpenandPrint)butthat'snotstrictlynecessary.ThismeansthatyoucantelltheOStotakeyourfileandcallwhatever'snecessarytoprintit.·Takescareofstandardfiletypes·Nonee
7、dtomessaroundwithprinterlists·Givesyounocontrol·Onlyworksforwell-defineddocument-applicationpairings.·OnlyprintstodefaultprinterUPDATE:KudostoChrisCurveyforpointingoutthatyoucanspecifyaprinterbyincludingitwithad:switchintheparamssection.Don'tknowifitworksfo
8、reveryfiletype.importtempfileimportwin32apiimportwin32printfilename=tempfile.mktemp(".txt")open(filename,"w").write("Thisisatest")win32api.ShellExecute(0,"print",filename,##IfthisisNone,thedefaultprint