文章目录
- 一、打印
- Windows 下打印 PDF
- 打印 Word
- 转换格式
一、打印
Windows 下打印 PDF
从 Windows 命令行打印 PDF
AcroRd32.exe /t "C:\Path\To\Your\File.pdf" "PrinterName"
# 其中,“C:\Path\To\Your\File.pdf”是您要打印的PDF文件的完整路径,“PrinterName”是您要使用的打印机的名称。
# 这将启动Acrobat Reader并自动将指定的PDF文件发送到打印机进行打印。请注意,“/t”选项将Acrobat Reader设置为“传统打印模式”,这意味着直接将文件发送到打印机而不会打开Acrobat Reader界面。
用Python实现PDF自动打印至打印机
PDFtoPrinter: Command-line PDF printing
# To print a PDF file to the default Windows printer, use this command:
PDFtoPrinter filename.pdf
# To print to a specific printer, add the name of the printer in quotation marks:
PDFtoPrinter filename.pdf "Name of Printer"
# If you want to print to a network printer, use the name that appears in Windows print
# dialogs, like this (and be careful to note the two backslashes at the start of the name and
# the single backslash after the servername):
PDFtoPrinter filename.pdf "\\SERVER\PrinterName"
打印 Word
Python:通过Win32模块操作Office对象之打印
def print_word():
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants
Word = EnsureDispatch("Word.Application") # 连接/创建Word对象(打开Word程序)
f = r"z:\123.docx"
doc = Word.Documents.Open(f) # 打开Word文档
doc.PrintOut() # 打印(到默认打印机);如果默认打印机为虚拟打印机,则会弹出保存文件对话框(需要选择保存文件的格式和路径)
doc.Close(constants.wdDoNotSaveChanges) # (不保存)关闭Word文档
Word.Quit() # 退出Word程序
def print_execl():
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants
Excel = EnsureDispatch("Excel.Application") # 打开Excel程序
f = r"z:\34.xlsx"
wb = Excel.Workbooks.Open(f) # 打开Excel工作簿
sht = wb.Sheets("Sheet1") # 指定工作表
sht.PrintOut() # 打印工作表
wb.Close(constants.xlDoNotSaveChanges) # (不保存)关闭工作簿
Excel.Quit() # 退出Excel程序
Java生成固定格式word并打印word文档解决方案【windows环境】
printword.exe
转换格式
Golang 实现word和Excel处理
package main
import (
ole "github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
func wordToPdf(fileName string) {
ole.CoInitialize(0)
unknown, _ := oleutil.CreateObject("Word.Application")
word, _ := unknown.QueryInterface(ole.IID_IDispatch)
oleutil.PutProperty(word, "Visible", false)
documents := oleutil.MustGetProperty(word, "Documents").ToIDispatch()
document := oleutil.MustCallMethod(documents, "Open", fileName).ToIDispatch()
oleutil.MustCallMethod(document, "SaveAs2", "z:/123_2.pdf", 17).ToIDispatch()
document.Release()
documents.Release()
word.Release()
ole.CoUninitialize()
}
func excelToPdf(fileName string) {
ole.CoInitialize(0)
unknown, _ := oleutil.CreateObject("Excel.Application")
excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
oleutil.PutProperty(excel, "Visible", false)
workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
workbook, _ := oleutil.CallMethod(workbooks, "Open", fileName)
//defer workbook.ToIDispatch().Release()
worksheet := oleutil.MustGetProperty(workbook.ToIDispatch(), "Worksheets", 1).ToIDispatch()
//defer worksheet.Release()
ps := oleutil.MustGetProperty(worksheet, "PageSetup").ToIDispatch()
oleutil.PutProperty(ps, "LeftHeader", "")
oleutil.PutProperty(ps, "CenterHeader", "")
oleutil.PutProperty(ps, "RightHeader", "")
oleutil.PutProperty(ps, "LeftFooter", "")
oleutil.PutProperty(ps, "CenterFooter", "")
oleutil.PutProperty(ps, "RightFooter", "")
oleutil.PutProperty(ps, "LeftMargin", 0)
oleutil.PutProperty(ps, "RightMargin", 0)
oleutil.PutProperty(ps, "TopMargin", 0)
oleutil.PutProperty(ps, "BottomMargin", 0)
oleutil.PutProperty(ps, "HeaderMargin", 0)
oleutil.PutProperty(ps, "FooterMargin", 0)
oleutil.PutProperty(ps, "Orientation", 2)
oleutil.PutProperty(ps, "Zoom", false)
oleutil.PutProperty(ps, "FitToPagesWide", 1)
oleutil.PutProperty(ps, "FitToPagesTall", false)
oleutil.PutProperty(ps, "CenterVertically", true)
oleutil.PutProperty(ps, "CenterHorizontally", true)
oleutil.PutProperty(ps, "Draft", false)
oleutil.PutProperty(ps, "FirstPageNumber", true)
oleutil.MustCallMethod(worksheet, "ExportAsFixedFormat", 0, "z:/34_2.pdf").ToIDispatch()
ps.Release()
worksheet.Release()
workbooks.Release()
excel.Release()
ole.CoUninitialize()
}
func main() {
wordToPdf("z:/123.docx")
excelToPdf("z:/34.xlsx")
}
Word VBA 參考 microsoft
Word 解决方案 microsoft
Python pywin32实现word和Excel的处理
pip install pywin32
def word_to_pdf():
from win32com import client as wc
pythoncom.CoInitialize()
file_path = 'Z:/123.docx'
try:
word = wc.gencache.EnsureDispatch('word.application')
except:
try:
word = wc.gencache.EnsureDispatch('kwps.application') # 如果使用wps
except:
word = wc.gencache.EnsureDispatch('wps.application') # 如果使用wps
newpdf = word.Documents.Open(file_path)
word.Visible = 0
newpdf.SaveAs(f'Z:/123.pdf', FileFormat=17)
newpdf.Close()
pythoncom.CoUninitialize()
def execl_to_pdf():
from win32com import client as wc
try:
excel = wc.DispatchEx('Excel.Application')
except:
try:
excel = wc.DispatchEx('ket.Application')
except:
excel = wc.DispatchEx('et.Application')
newpdf = excel.Workbooks.Open(r'Z:\34.xlsx')
excel.DisplayAlerts = 0
# 获取第一个sheet
all_sheets = [sheet.Name for sheet in newpdf.Sheets]
ws_source = newpdf.Worksheets(all_sheets[0])
# 设置页面设置
ws_source.PageSetup.LeftHeader = ""
ws_source.PageSetup.CenterHeader = ""
ws_source.PageSetup.RightHeader = ""
ws_source.PageSetup.LeftFooter = ""
ws_source.PageSetup.CenterFooter = ""
ws_source.PageSetup.RightFooter = ""
# ws_source.PageSetup.FitToPagesTall = 0
ws_source.PageSetup.FirstPageNumber = True
ws_source.PageSetup.LeftMargin = 0
ws_source.PageSetup.RightMargin = 0
ws_source.PageSetup.TopMargin = 0
ws_source.PageSetup.BottomMargin = 0
ws_source.PageSetup.HeaderMargin = 0
ws_source.PageSetup.FooterMargin = 0
# ws_source.PageSetup.PaperSize = 1
ws_source.PageSetup.Orientation = 2 # 横向转换pdf
ws_source.PageSetup.FitToPagesWide = 1 # 所有列压缩在一页纸
ws_source.PageSetup.FitToPagesTall = False
ws_source.PageSetup.Zoom = False # 所有列压缩在一页纸
ws_source.PageSetup.CenterVertically = True
ws_source.PageSetup.CenterHorizontally = True
ws_source.PageSetup.Draft = False
ws_source.Select()
# 行列自动调整
# ws_source.Columns.AutoFit()
# ws_source.Rows.AutoFit()
# 设置Excel的边框
rows = ws_source.UsedRange.Rows.Count
cols = ws_source.UsedRange.Columns.Count
ws_source.Range(ws_source.Cells(1, 1), ws_source.Cells(rows, cols)).Borders.LineStyle = 1
ws_source.Range(ws_source.Cells(1, 1), ws_source.Cells(rows, cols)).Borders.TintAndShade = 0
ws_source.Range(ws_source.Cells(1, 1), ws_source.Cells(rows, cols)).Borders.Weight = 1
# 转换为PDF文件
newpdf.ExportAsFixedFormat(0, r'Z:\34.pdf')
newpdf.Close()
excel.Quit()
Python的Pywin32库:简化Windows编程的强大工具
如何使用Golang生成和转换PDF文档
golang打印机控制,go 打印机