自动打印现有的MS Access报告(最好使用Python,但可以选择替代方法) [英] automate printing of existing MS Access report (preferably with Python, but open to alternatives)

查看:32
本文介绍了自动打印现有的MS Access报告(最好使用Python,但可以选择替代方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大约有30个.accdb文件,所有文件都包含一个具有相同名称的报告.我想自动将报告另存为PDF,以供后续打印.仅此而已,无需查询,无需修改,只需将保存的报告打印为PDF.

I have about 30 .accdb files, all contain a report with the same name. I'd like to automate saving the reports as PDFs for subsequent printing. That's all, no queries, no modification, just print the saved report to a PDF.

我可以成功打开数据库,但是我不确定该如何做.

I can open the db successfully, but I'm not sure how to do the rest.

import win32com.client

a = win32com.client.Dispatch("Access.Application")
a.visible = 1  
filename = r'C:\bla\Exam_1\PrintTest\db.accdb'
db = a.OpenCurrentDatabase(filename)

report_name = 'My_Report'

a.Quit()

FWIW,我是位老师,这将帮助我评分论文,并减轻考试结束时压力大的学生的负担.使用Windows 10.

FWIW, I'm a teacher, and this will help me with grading submissions and take the burden off stressed students printing at the end of an exam. Using Windows 10.

我愿意使用任何其他脚本语言(powershell?)来帮助我轻松地自动完成此任务.

I'm open to using any other scripting language (powershell?) that will help me easily automate this task.

推荐答案

您可以使用DoCmd.OpenReport自动打开和打印报告.

You can use DoCmd.OpenReport to automatically open and print reports.

import win32com.client

a = win32com.client.Dispatch("Access.Application")
a.visible = 1  
filename = r'C:\bla\Exam_1\PrintTest\db.accdb'
db = a.OpenCurrentDatabase(filename)

report_name = 'My_Report'
a.DoCmd.OpenReport('My_Report')
a.Quit()

默认情况下,DoCmd.OpenReport将把报告打印到报告设置中指定的打印机上.我周围有代码可以在打印预览中打开它,并指定打印机.

By default, DoCmd.OpenReport will print the report to the printer specified in the report settings. I have code lying around to open it in print preview and specify a printer as well.

如果要导出为PDF,也可以将其自动化:

If you want to export to a PDF, you can automate that as well:

import win32com.client

a = win32com.client.Dispatch("Access.Application")
a.visible = 1  
filename = r'C:\bla\Exam_1\PrintTest\db.accdb'
db = a.OpenCurrentDatabase(filename)

report_name = 'My_Report'
a.DoCmd.OutputTo(3, report_name, r'PDF Format (*.pdf)', r'C:\Path\To\file.pdf')
a.Quit()

这篇关于自动打印现有的MS Access报告(最好使用Python,但可以选择替代方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆