我之前在这里提出了这个问题:pypdf2-merging-pdf-pages-issue
现在我已经走了很长一段路,现在可以通过Pandas将Excel文档中的PDF文件创建为PyPDF2。
以及现在我必须拥有的每页数量。PDF。但是,我现在的问题是合并的PDF文件现在为空白。
如果进行调试,则可以在第二个循环中看到该循环,其中包含变量“ paths”是指向物理PDF文件的正确路径。但是,当他们然后通过:
with path.open('rb') as pdf:
pdf_writer.append(pdf)
然后突然有一个额外的“”输入路径,因此路径可以命名为c:\ users \...。然后突然将其称为c:\ users \ ...
不知道这是什么导致文件无法正确打开和读取,然后合并为一个PDF文件。
希望有人可以指导我,因为python对我来说是自学的。或者以其他方式可以向我解释为什么我创建了一些合并的PDF文件,这些文件在3页上突然空白。
我的代码是:
import datetime #Handle date
import pandas as pd #Handle data from Excel Sheet (Data analysis)
import PyPDF2 as pdf2 #Handle PDF read and merging
from pathlib import Path #Handle path
#Skip ERROR-message: Xref table not zero-indexed. ID numbers for objects will be corrected.
#import sys
#if not sys.warnoptions:
# import warnings
# warnings.simplefilter("ignore")
PDF_PATH = Path('C:/Users/TH/PDF/')
EXCEL_FILENAME = 'Resources/liste.xlsx'
def main():
today = datetime.date.today() # The date now
next_week = today.isocalendar()[1] + 1 # 0=Year, 1=week
resources = pd.read_excel(EXCEL_FILENAME, sheet_name='Ark1')
for row in resources.itertuples():
year = row.Aargang
paths = [
(PDF_PATH / row.Oevelse1).with_suffix('.pdf'),
(PDF_PATH / row.Oevelse2).with_suffix('.pdf'),
(PDF_PATH / row.Oevelse3).with_suffix('.pdf'),
]
pdf_writer = pdf2.PdfFileMerger()
for path in paths:
with path.open('rb') as pdf:
pdf_writer.append(pdf)
with open(f'Uge {next_week} - {year} Merged_doc.pdf', 'wb') as output:
pdf_writer.write(output)
if __name__ == '__main__':
main()
@ anon01 Thx
并感谢小天狼星3。
关于PyPDF2,如何使用它以及它的一些错误。因此,在将代码编辑为此后,它就可以工作了。
import datetime #Handle date
import pandas as pd #Handle data from Excel Sheet (Data analysis)
from PyPDF2 import PdfFileMerger #Handle PDF read and merging
from pathlib import Path #Handle path
#Skip ERROR-message: Xref table not zero-indexed. ID numbers for objects will be corrected.
#import sys
#if not sys.warnoptions:
# import warnings
# warnings.simplefilter("ignore")
PDF_PATH = Path('C:/Users/TH/PDF')
EXCEL_FILENAME = 'Resources/liste.xlsx'
def main():
today = datetime.date.today() # The date now
next_week = today.isocalendar()[1] + 1 # 0=Year, 1=week
resources = pd.read_excel(EXCEL_FILENAME, sheet_name='Ark1')
for row in resources.itertuples():
year = row.Aargang
paths = [
(PDF_PATH / row.Oevelse1).with_suffix('.pdf'),
(PDF_PATH / row.Oevelse2).with_suffix('.pdf'),
(PDF_PATH / row.Oevelse3).with_suffix('.pdf'),
]
pdf_merger = PdfFileMerger()
for path in paths:
pdf_merger.append(str(path))
with open(f'Uge {next_week} - {year} Merged_doc.pdf', 'wb') as output:
pdf_merger.write(output)
pdf_merger.close()
if __name__ == '__main__':
main()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句