Script to add bookmarks to a PDF (Need review and suggestions)
Hello guys,
So i made this little script that adds bookmarks to PDFs using Py2PDF module. It's not really a full automation of the process of adding bookmarks since you still have to manually add the parent bookmark and add the bookmarks (Chapters + their respectives pages) into a dictionnary. Once the dictionnary is made, the bookmarks are added with a for loop that uses the addBookmark method in a new output PDF file.
from PyPDF2 import PdfFileWriter, PdfFileReader
pdf_object = open("pdf_without_bookmarks.pdf","rb") #rb stands for read binary
output = PdfFileWriter()
input = PdfFileReader(pdf_object)
input_numpages = input.getNumPages()
#basically just copy the input file
for i in range(input_numpages):
output.addPage(input.getPage(i)) #insert page in the output file
parent_1 = output.addBookmark('Parent 2', page of parent 2) # add parent bookmark
bookmarks_dic_1 = {
"Chapter 1": page of Chapter 1,
"Chapter 2": page of Chapter 2,
"Chapter n": page of Chapter n,
}
for k, v in bookmarks_dic_1.items():
output.addBookmark(k ,v +1,parent_1 ) #add child bookmarks
parent_2 = output.addBookmark('Parent 2', page of parent 2)
bookmarks_dic_2 = {
"Chapter 1": page of Chapter 1,
"Chapter 2": page of Chapter 2,
"Chapter n": page of Chapter n,
}
for k, v in bookmarks_dic_2.items():
output.addBookmark(k ,v +1,parent_2 ) #add child bookmarks
outputstream = open('pdf_with_bookmarks.pdf','wb') #creating result
output.write(outputstream) #writing to result pdf
outputstream.close() #closing result
Initially, when i started working on this, i wanted to scrape Chapter and Page information from PDF filesummary, but i realized it was too ambitious and that it requires to know how to use regex (Am i right?).
I also want to know if there's a way to put all this in one loop, instead of having to create a new loop for each parent. But i don't know how. Like is there such thing as a dictionnary with more than 1 value? The Parent will be the key and the values Name of chapter and Page of chapter. A kind of a double dictionnary. Something like this :
summary_dictionnary = {"Parent 1":{"Chapter 1": Page, "Chapter n": Page}, "Parent n": {"Chapter 1": Page,"Chapter n":Page}}
Is that even possible?
I'm also posting here to have a general review on my code, what do you guys would change?
Big thanks in advance
/r/Python
https://redd.it/7tih40
Hello guys,
So i made this little script that adds bookmarks to PDFs using Py2PDF module. It's not really a full automation of the process of adding bookmarks since you still have to manually add the parent bookmark and add the bookmarks (Chapters + their respectives pages) into a dictionnary. Once the dictionnary is made, the bookmarks are added with a for loop that uses the addBookmark method in a new output PDF file.
from PyPDF2 import PdfFileWriter, PdfFileReader
pdf_object = open("pdf_without_bookmarks.pdf","rb") #rb stands for read binary
output = PdfFileWriter()
input = PdfFileReader(pdf_object)
input_numpages = input.getNumPages()
#basically just copy the input file
for i in range(input_numpages):
output.addPage(input.getPage(i)) #insert page in the output file
parent_1 = output.addBookmark('Parent 2', page of parent 2) # add parent bookmark
bookmarks_dic_1 = {
"Chapter 1": page of Chapter 1,
"Chapter 2": page of Chapter 2,
"Chapter n": page of Chapter n,
}
for k, v in bookmarks_dic_1.items():
output.addBookmark(k ,v +1,parent_1 ) #add child bookmarks
parent_2 = output.addBookmark('Parent 2', page of parent 2)
bookmarks_dic_2 = {
"Chapter 1": page of Chapter 1,
"Chapter 2": page of Chapter 2,
"Chapter n": page of Chapter n,
}
for k, v in bookmarks_dic_2.items():
output.addBookmark(k ,v +1,parent_2 ) #add child bookmarks
outputstream = open('pdf_with_bookmarks.pdf','wb') #creating result
output.write(outputstream) #writing to result pdf
outputstream.close() #closing result
Initially, when i started working on this, i wanted to scrape Chapter and Page information from PDF filesummary, but i realized it was too ambitious and that it requires to know how to use regex (Am i right?).
I also want to know if there's a way to put all this in one loop, instead of having to create a new loop for each parent. But i don't know how. Like is there such thing as a dictionnary with more than 1 value? The Parent will be the key and the values Name of chapter and Page of chapter. A kind of a double dictionnary. Something like this :
summary_dictionnary = {"Parent 1":{"Chapter 1": Page, "Chapter n": Page}, "Parent n": {"Chapter 1": Page,"Chapter n":Page}}
Is that even possible?
I'm also posting here to have a general review on my code, what do you guys would change?
Big thanks in advance
/r/Python
https://redd.it/7tih40
reddit
Script to add bookmarks to a PDF (Need review and... • r/Python
Hello guys, So i made this little script that adds bookmarks to PDFs using Py2PDF module. It's not really a full automation of the process of...
Submitting more than one form
Hey there,
I'm trying to submit two crispy forms at the same time, but just can't seem to figure out how to do that, my submit button completely breaks when there is more than 1 form. Is someone having an idea how I can do it?
My HTML:
{% block content %}
/// Some tags to style the page ///
<div>
<form method="post" action="" class="form">{% csrf_token %}
{% crispy form %}
{% crispy item_form %}
<input type="button" value="Add More" id="add_more">
<script>
$('#add_more').click(function() {
cloneMore('div.table:last', 'service');
});
</script>
<div class="box-footer">
<div class="form-actions">
<input type="submit" class="btn btn-primary btn-flat pull-right" value='Create'>
</div>
</div>
</form>
</div>
{% endblock %}
And my views:
class OfferCreateView(CreateView):
def dispatch(self, request, *args, **kwargs):
return super(OfferCreateView, self).dispatch(request, *args, **kwargs)
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.last_modified_by = self.request.user
self.object.save()
form.save_m2m()
return redirect(self.object.get_absolute_url())
model = Offer
template_name = 'offers/offer_create.html'
form_class = OfferCreateForm
def get(self, request, *args, **kwargs):
form = OfferCreateForm(request=request)
itemformset = formset_factory(ItemCreateForm)
item_form = itemformset()
return self.render_to_response({
"form": form,
"item_form": item_form
})
/r/django
https://redd.it/8mbdyr
Hey there,
I'm trying to submit two crispy forms at the same time, but just can't seem to figure out how to do that, my submit button completely breaks when there is more than 1 form. Is someone having an idea how I can do it?
My HTML:
{% block content %}
/// Some tags to style the page ///
<div>
<form method="post" action="" class="form">{% csrf_token %}
{% crispy form %}
{% crispy item_form %}
<input type="button" value="Add More" id="add_more">
<script>
$('#add_more').click(function() {
cloneMore('div.table:last', 'service');
});
</script>
<div class="box-footer">
<div class="form-actions">
<input type="submit" class="btn btn-primary btn-flat pull-right" value='Create'>
</div>
</div>
</form>
</div>
{% endblock %}
And my views:
class OfferCreateView(CreateView):
def dispatch(self, request, *args, **kwargs):
return super(OfferCreateView, self).dispatch(request, *args, **kwargs)
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.last_modified_by = self.request.user
self.object.save()
form.save_m2m()
return redirect(self.object.get_absolute_url())
model = Offer
template_name = 'offers/offer_create.html'
form_class = OfferCreateForm
def get(self, request, *args, **kwargs):
form = OfferCreateForm(request=request)
itemformset = formset_factory(ItemCreateForm)
item_form = itemformset()
return self.render_to_response({
"form": form,
"item_form": item_form
})
/r/django
https://redd.it/8mbdyr
reddit
Submitting more than one form • r/django
Hey there, I'm trying to submit two crispy forms at the same time, but just can't seem to figure out how to do that, my submit button completely...
🌍EOmaps v6.5 released!
I just released EOmaps v6.5!
EOmaps is a python module to create maps of of geographical datasets and use them as interactive data-analysis widgets.
Now you can add GridLabels in arbitrary projections!
https://preview.redd.it/dt6gkryb8v0b1.png?width=1423&format=png&auto=webp&v=enabled&s=2d60c11c4830ead873cbc50221530a0154d3da6b
/r/Python
https://redd.it/13ma54g
I just released EOmaps v6.5!
EOmaps is a python module to create maps of of geographical datasets and use them as interactive data-analysis widgets.
Now you can add GridLabels in arbitrary projections!
https://preview.redd.it/dt6gkryb8v0b1.png?width=1423&format=png&auto=webp&v=enabled&s=2d60c11c4830ead873cbc50221530a0154d3da6b
/r/Python
https://redd.it/13ma54g
GitHub
GitHub - raphaelquast/EOmaps: A library to create interactive maps of geographical datasets
A library to create interactive maps of geographical datasets - raphaelquast/EOmaps