【发布时间】:2021-12-27 18:24:48
【问题描述】:
我有一堆 Word 文档需要重置为 Letter 页面大小(它们目前都是 11"x17")。有没有我可以使用的 Python 库:
- 加载 Word 文档
- 将其页面大小设置为 Letter
- 打印成 PDF
似乎docx2pdf 可以用于执行 1 和 3,但是 2 呢?如果它可以做到,怎么做?其他选择?
TIA!
【问题讨论】:
我有一堆 Word 文档需要重置为 Letter 页面大小(它们目前都是 11"x17")。有没有我可以使用的 Python 库:
似乎docx2pdf 可以用于执行 1 和 3,但是 2 呢?如果它可以做到,怎么做?其他选择?
TIA!
【问题讨论】:
这就是最后对我有用的方法:
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)
【讨论】: