本文整理匯總了Python中pdfrw.PdfWriter方法的典型用法代碼示例。如果您正苦於以下問題:Python pdfrw.PdfWriter方法的具體用法?Python pdfrw.PdfWriter怎麽用?Python pdfrw.PdfWriter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pdfrw
的用法示例。
在下文中一共展示了pdfrw.PdfWriter方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: set_pagelabel
# 需要導入模塊: import pdfrw [as 別名]
# 或者: from pdfrw import PdfWriter [as 別名]
def set_pagelabel(self,count,style="arabic"):
if self.isPDF:
from pdfrw import PdfReader, PdfWriter
from pagelabels import PageLabels, PageLabelScheme
reader = PdfReader(self.filename)
labels = PageLabels.from_pdf(reader)
newlabels = PageLabels()
for label in labels:
if label.startpage != self.page:
newlabels.append(label)
newlabel = PageLabelScheme(startpage=self.page,
style=style,
prefix="",
firstpagenum=count)
newlabels.append(newlabel)
newlabels.write(reader)
writer = PdfWriter()
writer.trailer = reader
logging.debug("writing new pagelabels...")
writer.write(self.filename)
# unused; using pdfrw instead
示例2: merge
# 需要導入模塊: import pdfrw [as 別名]
# 或者: from pdfrw import PdfWriter [as 別名]
def merge(overlay, basename):
'''
Merge the overlay with the original form, and return the result.
'''
input_pdf_path = os.path.join('templates', basename)
template_pdf = pdfrw.PdfReader(input_pdf_path)
overlay_pdf = pdfrw.PdfReader(overlay)
for page, data in zip(template_pdf.pages, overlay_pdf.pages):
overlay = pdfrw.PageMerge().add(data)[0]
pdfrw.PageMerge(page).add(overlay).render()
form = io.BytesIO()
pdfrw.PdfWriter().write(form, template_pdf)
form.seek(0)
return form
示例3: write
# 需要導入模塊: import pdfrw [as 別名]
# 或者: from pdfrw import PdfWriter [as 別名]
def write(self, filename=None, overwrite=False):
if filename is None and not overwrite:
raise ValueError(
'Must specify either output filename or overwrite flag'
)
if overwrite:
filename = self._filename
writer = PdfWriter(
version=self._pdf.pdf_version,
compress=self._compress,
)
writer.write(fname=filename, trailer=self._pdf._reader)
示例4: do_buttons
# 需要導入模塊: import pdfrw [as 別名]
# 或者: from pdfrw import PdfWriter [as 別名]
def do_buttons(basename, data_dict, keyfile):
'''
Some buttons are tricky because they're linked together
by a single parent element that we need to update.
Handle that case here.
'''
input_pdf_path = os.path.join('filled', basename)
output_pdf_path = os.path.join('filled', basename)
template_pdf = pdfrw.PdfReader(input_pdf_path)
annotations = template_pdf.pages[0][ANNOT_KEY]
value_to_readable = parse_keyfile(keyfile)
for page in template_pdf.pages:
annotations = page[ANNOT_KEY]
if annotations is None:
continue
for annotation in annotations:
if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
if not annotation[ANNOT_FIELD_KEY]:
parent_key = annotation[PARENT_KEY][ANNOT_FIELD_KEY]
if parent_key is None:
continue
parent_key = parent_key[1:-1]
if parent_key not in value_to_readable:
continue
if annotation['/AS']:
if 'BUTTONS' in data_dict:
for k, v in data_dict['BUTTONS']:
if k == value_to_readable[parent_key] and '/' + v in annotation['/AP']['/D'].keys():
annotation[PARENT_KEY].update(pdfrw.PdfDict(V=v))
pdfrw.PdfWriter().write(output_pdf_path, template_pdf)
示例5: _make_pdf_pdfrw
# 需要導入模塊: import pdfrw [as 別名]
# 或者: from pdfrw import PdfWriter [as 別名]
def _make_pdf_pdfrw(fields: dict, src_pdf: str, basename: str, flatten: bool=False):
"""Backup make_pdf function in case pdftk is not available."""
template = pdfrw.PdfReader(src_pdf)
# Different types of PDF fields
BUTTON = '/Btn'
# Names for entries in PDF annotation list
DEFAULT_VALUE = '/DV'
APPEARANCE = '/MK'
FIELD = '/T'
PROPS = '/P'
TYPE = '/FT'
FLAGS = '/Ff'
SUBTYPE = '/Subtype'
ALL_KEYS = ['/DV', '/F', '/FT', '/Ff', '/MK', '/P', '/Rect',
'/Subtype', '/T', '/Type']
annots = template.pages[0]['/Annots']
# Update each annotation if it's in the requested dictionary
for annot in annots:
this_field = annot[FIELD][1:-1]
# Check if the field has a new value passed
if this_field in fields.keys():
val = fields[this_field]
# Convert integers to strings
if isinstance(val, int):
val = str(val)
log.debug(f"Set field '{this_field}' "
f"({annot[TYPE]}) "
f"to `{val}` ({val.__class__}) "
f"in file '{basename}.pdf'")
# Prepare a PDF dictionary based on the fields properties
if annot[TYPE] == BUTTON:
# Radio buttons require special appearance streams
if val == CHECKBOX_ON:
val = bytes(val, 'utf-8')
pdf_dict = pdfrw.PdfDict(V=val, AS=val)
else:
continue
else:
# All other widget types
pdf_dict = pdfrw.PdfDict(V=val)
annot.update(pdf_dict)
else:
log.debug(f"Skipping unused field '{this_field}' in file '{basename}.pdf'")
# Now write the PDF to the new pdf file
pdfrw.PdfWriter().write(f'{basename}.pdf', template)