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)
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()