Last active
May 14, 2022 18:51
-
-
Save Mufanc/89ffa7af1ade87ea49c2f1f562239b55 to your computer and use it in GitHub Desktop.
[Python 合成 pdf] 多张图片合并成一个 pdf #Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
from fpdf import FPDF | |
pdf_file = FPDF(unit='pt', format=(2112, 3158)) # 页面尺寸 | |
pdf_file.set_auto_page_break(False) # 关闭自动分页 | |
pdf_file.set_left_margin(0) # 设置页边距 | |
pdf_file.set_top_margin(0) | |
images = os.listdir('images/') | |
images.sort(key=lambda s: int(re.search(r'^(\d+)', s).group(1))) | |
for file in images: | |
pdf_file.add_page() | |
print(os.path.join('images/', file)) | |
pdf_file.image(os.path.join('images/', file)) | |
pdf_file.output('output.pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment