【问题标题】:Python: how do I change page size of a Word document and then print it into PDF?Python:如何更改 Word 文档的页面大小,然后将其打印成 PDF?
【发布时间】:2021-12-27 18:24:48
【问题描述】:

我有一堆 Word 文档需要重置为 Letter 页面大小(它们目前都是 11"x17")。有没有我可以使用的 Python 库:

  1. 加载 Word 文档
  2. 将其页面大小设置为 Letter
  3. 打印成 PDF

似乎docx2pdf 可以用于执行 1 和 3,但是 2 呢?如果它可以做到,怎么做?其他选择?

TIA!

【问题讨论】:

    标签: python pdf docx


    【解决方案1】:

    这就是最后对我有用的方法:

    from docx import Document
    from docx.shared import Inches
    import os
    from docx2pdf import convert
    
    for root, dirs, files in os.walk(r"c:\rootdir"):
      for file in files:
        if file.endswith('.docx'):
            inDocxFN = os.path.join(root, file)
    
            base = os.path.splitext(inDocxFN)[0]
            outDocxFN = base + '.2.docx'
            outPdfFN = base + '.pdf'
            print("%s TO %s" % (inDocxFN,outPdfFN))
    
            document = Document(inDocxFN)
    
            section = document.sections[0]
    
            section.page_width = 7772400
            section.page_height = 10058400
    
            # don't want to modify the originals, so create a copy and then generate PDF from it, then delete the copy
            #
            document.save(outDocxFN)
    
            try:
                 convert(outDocxFN,outPdfFN)
            except:
                 print("Failed converting %s" % outDocxFN)
                 
            try:
                 os.remove(outDocxFN)
            except:
                 print("Failed deleting %s" % outDocxFN)
             
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 2020-04-27
      相关资源
      最近更新 更多