我有一堆 Word 檔案需要重置為 Letter 頁面大小(它們目前都是 11"x17")。是否有一個 Python 庫可以用來:
- 加載 Word 檔案
- 將其頁面大小設定為 Letter
- 列印成PDF
似乎docx2pdf可以用來做 1 和 3 但那 2 呢?如果它可以做到,如何做到?其他選擇?
蒂亞!
uj5u.com熱心網友回復:
以下是最后對我有用的內容:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365510.html
