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

Python+excel series: case 6: batch printing workbooks, batch printing specified worksheets in multiple workbooks

編輯:Python

List of articles

  • Batch print workbooks
  • Batch print specified worksheets in multiple workbooks

Batch print workbooks

Print specific functions PrintOut()

import os # Import os modular 
import xlwings as xw # Import xlwings modular 
file_path = 'e:/table' # Give the path to the folder where the workbook to be printed is located 
file_list = os.listdir(file_path) # List the names of all files and subfolders under the folder 
app = xw.App(visible=False,add_book=False)
for i in file_list:
if i.startswith('~$'):# Determine whether there is a file name with “~$” Opening file 
continue # If there is , Skip this type of file 
file_paths = os.path.join(file_path,i) # Get the file path of the workbook to be printed 
workbook = app.books.open(file_paths) # Open the workbook to print 
workbook.api.PrintOut() # Print the workbook 
app.quit()

because xlwings Module does not provide a function to print workbooks , So the first 11 Line code takes advantage of the... Of the workbook object api Calling a VBA Of PrintOut() Function to print the workbook , The syntax format and common parameters of this function are as follows :

PrintOut(From,To,Copies,Preview,ActivePrinter,PrintToFile,Collate,PrToFile)

Batch print specified worksheets in multiple workbooks

import os # Import os modular 
import xlwings as xw # Import xlwings modular 
file_path = 'e:/table' # Give the path to the folder where the workbook to be printed is located 
file_list = os.listdir(file_path) # List the names of all files and subfolders under the folder 
sheet_name = 'sheetX' # Give the name of the worksheet to print 
app = xw.App(visible=False,add_book=False)
for i in file_list:
if i.startswith('~$'):# Determine whether there is a file name with “~$” Opening file 
continue # If there is , Skip this type of file 
file_paths = os.path.join(file_path,i) # Get the file path of the workbook to be printed 
workbook = app.books.open(file_paths) # Open the workbook to print 
for j in workbook.sheets:
if j.name == sheet_name: # Determine whether there is a workbook named “sheetX” The worksheet for 
j.api.PrintOut() # If there is , Print the worksheet 
break
app.quit()

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