【问题标题】:Getting in python in advance the number of pages after printing a document提前在python中获取打印文档后的页数
【发布时间】:2017-01-05 18:02:00
【问题描述】:

我的打印机不支持双面打印,因此我基于lpr 实用程序用Python 编写了一个简单的脚本。它会延迟逐页打印文档,在此期间我手动翻纸:

from sys import argv
from subprocess import run

assert(len(argv) == 2)

i = 1
try:
  while True:
    opt = 'page-ranges={}'.format(i)
    i += 1
    print(run(['lpr', '-o', opt, argv[1]]))
    f = input('> Ready to print next page?\n'
              '(Press ENTER or CTRL+C to exit) ')
except KeyboardInterrupt:
  print()

问题是我事先不知道最终打印文档的页数,所以不知道什么时候应该中断while循环。

也许有解决方案可以帮助我获得结果页面的数量?

附:我使用的是 macOS Sierra 10.12.2 和 A4 纸。

【问题讨论】:

  • @sabbahillel 它不适用于 pdf、docx 等。
  • 您是否能够打印到文本文件并在实际发送到输出之前使用 wc 获取文本文件的大小?

标签: python macos unix printing printers


【解决方案1】:

计数pdf

from pyPdf import PdfFileReader
pdf = PdfFileReader(open('path/to/file.pdf','rb'))
pdf.getNumPages()

统计文档

from win32com.client import Dispatch
#open Word
word = Dispatch('Word.Application')
word.Visible = False
word = word.Documents.Open(doc_path)

#get number of sheets
word.Repaginate()
num_of_sheets = word.ComputeStatistics(2)

请参阅以下页面了解更多详情 感谢@Josh 和@a_guest

pyPDF - Retrieve page numbers from document

Number of pages of a word document with Python

【讨论】:

  • 这是一个很好的解决方法,但我希望有一个通用的解决方案:)
猜你喜欢
  • 2014-08-28
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 2014-08-25
  • 2011-03-29
相关资源
最近更新 更多