Python: 如何更改Word文档的页面大小,然后将其打印成PDF?
Python: how do I change page size of a Word document and then print it into PDF?
我有一堆 Word 文档需要重置为 Letter 页面大小(它们目前都是 11"x17")。有没有 Python 库可以用来:
- 加载 Word 文档
- 将其页面大小设置为 Letter
- 将其打印成 PDF
似乎 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)
我有一堆 Word 文档需要重置为 Letter 页面大小(它们目前都是 11"x17")。有没有 Python 库可以用来:
- 加载 Word 文档
- 将其页面大小设置为 Letter
- 将其打印成 PDF
似乎 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)