用python用打印机打印EXCEL表格

转发https://blog.csdn.net/weixin_48160417/article/details/125395557
在此记录自己使用

打印一个工作簿中的所有工作表

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值