用python对excel进行打印操作

本文代码及数据集来自《超简单:用Python让Excel飞起来(实战150例)》

# 打印一个工作簿中的所有工作表
import xlwings as xw
app = xw.App(visible=False, add_book=False)
workbook = app.books.open('各月销售数量表.xlsx')
workbook.api.PrintOut(Copies=2, ActivePrinter='DESKTOP-HP01', Collate=True) # 打印工作簿中的所有工作表,这里指定打印份数为两份,打印机为“DESKTOP-HP01”
workbook.close()
app.quit()

# 打印一个工作簿中的一个工作表
import xlwings as xw
app = xw.App(visible=False, add_book=False)
workbook = app.books.open('各月销售数量表.xlsx')
worksheet = workbook.sheets['1月']
worksheet.api.PrintOut(Copies=2, ActivePrinter='DESKTOP-HP01', Collate=True)
workbook.close()
app.quit()

# 打印多个工作簿
from pathlib import Path
import xlwings as xw
folder_path = Path('各地区销售数量')
file_list = folder_path.glob('*.xls*')
app = xw.App(visible=False, add_book=False)
for i in file_list:
    workbook = app.books.open(i)
    workbook.api.PrintOut(Copies=1, ActivePrinter='DESKTOP-HP01', Collate=True)
    workbook.close()
app.quit()

# 打印多个工作簿中的同名工作表
from pathlib import Path
import xlwings as xw
folder_path = Path('各地区销售数量')
file_list = folder_path.glob('*.xls*')
app = xw.App(visible=False, add_book=False)
for i in file_list:
    workbook = app.books.open(i)
    worksheet = workbook.sheets['销售数量']
    worksheet.api.PrintOut(Copies=1, ActivePrinter='DESKTOP-HP01', Collate=True)
    workbook.close()
app.quit()

# 打印工作表的指定单元格区域
import xlwings as xw
app = xw.App(visible=False, add_book=False)
workbook = app.books.open('销售表.xlsx')
worksheet = workbook.sheets['总表']
area = worksheet.range('A1:I10')
area.api.PrintOut(Copies=1, ActivePrinter='DESKTOP-HP01', Collate=True)
workbook.close()
app.quit()

# 按指定的缩放比例打印工作表
import xlwings as xw
app = xw.App(visible=False, add_book=False)
workbook = app.books.open('销售表.xlsx')
worksheet = workbook.sheets['总表']
worksheet.api.PageSetup.Zoom = 80 # 按工作表原始大小的80%进行打印
worksheet.api.PrintOut(Copies=1, ActivePrinter='DESKTOP-HP01', Collate=True)
workbook.close()
app.quit()

# 在纸张的居中位置打印工作表
import xlwings as xw
app = xw.App(visible=False, add_book=False)
workbook = app.books.open('各月销售数量表.xlsx')
worksheet = workbook.sheets['1月']
worksheet.api.PageSetup.CenterHorizontally = True # 调整工作表在纸张上的水平位置
worksheet.api.PageSetup.CenterVertically = True # 调整工作表在纸张上的垂直位置
worksheet.api.PrintOut(Copies=1, ActivePrinter='DESKTOP-HP01', Collate=True)
workbook.close()
app.quit()

# 打印工作表时打印行号和列号
import xlwings as xw
app = xw.App(visible=False, add_book=False)
workbook = app.books.open('各月销售数量表.xlsx')
worksheet = workbook.sheets['1月']
worksheet.api.PageSetup.PrintHeadings = True
worksheet.api.PrintOut(Copies=1, ActivePrinter='DESKTOP-HP01', Collate=True)
workbook.close()
app.quit()

# 重复打印工作表的标题行
import xlwings as xw
app = xw.App(visible=False, add_book=False)
workbook = app.books.open('销售表.xlsx')
worksheet = workbook.sheets['总表']
worksheet.api.PageSetup.PrintTitleRows = '$1:$1' # 将工作表第一行设置为要重复打印的标题行
worksheet.api.PageSetup.Zoom = 55 # 按工作表原始大小的55%进行打印
worksheet.api.PrintOut(Copies=1, ActivePrinter='DESKTOP-HP01', Collate=True)
workbook.close()
app.quit()

你可能感兴趣的:(用python处理excel,python,excel)