-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8ad02b
commit 4edab2d
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# DOCXtoPDF.py | ||
|
||
# Author: Vasudev Ram - http://www.dancingbison.com | ||
# Copyright 2012 Vasudev Ram, http://www.dancingbison.com | ||
|
||
# This is open source code, released under the New BSD License - | ||
# see http://www.opensource.org/licenses/bsd-license.php . | ||
|
||
# This program uses the python-docx library, available at: | ||
# https://github.com/mikemaccana/python-docx | ||
|
||
import sys | ||
import os | ||
import os.path | ||
import string | ||
from textwrap import TextWrapper | ||
from docx import opendocx, getdocumenttext | ||
from PDFWriter import PDFWriter | ||
|
||
def docx_to_pdf(infilename, outfilename): | ||
|
||
# Extract the text from the DOCX file object infile and write it to | ||
# a PDF file. | ||
|
||
try: | ||
infil = opendocx(infilename) | ||
except: | ||
print("Error opening infilename") | ||
#print "Exception: " + repr(e) + "\n" | ||
sys.exit(1) | ||
|
||
paragraphs = getdocumenttext(infil) | ||
|
||
pw = PDFWriter(outfilename) | ||
pw.setFont("Courier", 12) | ||
pw.setHeader("DOCXtoPDF - convert text in DOCX file to PDF") | ||
pw.setFooter("Generated by xtopdf and python-docx") | ||
wrapper = TextWrapper(width=70, drop_whitespace=False) | ||
|
||
# For Unicode handling. | ||
new_paragraphs = [] | ||
for paragraph in paragraphs: | ||
new_paragraphs.append(paragraph.encode("utf-8")) | ||
|
||
for paragraph in new_paragraphs: | ||
lines = wrapper.wrap(paragraph) | ||
for line in lines: | ||
pw.writeLine(line) | ||
pw.writeLine("") | ||
|
||
pw.savePage() | ||
pw.close() | ||
|
||
def usage(): | ||
|
||
return "Usage: python DOCXtoPDF.py infile.docx outfile.txt\n" | ||
|
||
def main(): | ||
|
||
try: | ||
# Check for correct number of command-line arguments. | ||
if len(sys.argv) != 3: | ||
print ("Wrong number of arguments") | ||
#print usage() | ||
sys.exit(1) | ||
infilename = sys.argv[1] | ||
outfilename = sys.argv[2] | ||
|
||
# Check for right infilename extension. | ||
infile_ext = os.path.splitext(infilename)[1] | ||
if infile_ext.upper() != ".DOCX": | ||
print ("Input filename extension should be .DOCX") | ||
#print usage() | ||
sys.exit(1) | ||
|
||
# Check for right outfilename extension. | ||
outfile_ext = os.path.splitext(outfilename)[1] | ||
if outfile_ext.upper() != ".PDF": | ||
print( "Output filename extension should be .PDF") | ||
#print usage() | ||
sys.exit(1) | ||
|
||
docx_to_pdf(infilename, outfilename) | ||
|
||
except: | ||
sys.stderr.write("Error: " + repr(e) + "\n") | ||
sys.exit(1) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
||
# EOF |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import urllib.request | ||
import random | ||
|
||
def downloader(image_url): | ||
file_name = random.randrange(1,10000) | ||
full_file_name = str(file_name) + '.jpg' | ||
urllib.request.urlretrieve(image_url,full_file_name) | ||
|
||
url ="http://www.binarynote.com/wp-content/themes/binarynote3/images/main.jpg" | ||
downloader(url) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from PIL import Image, ImageDraw | ||
def main(): | ||
# Open the original image | ||
main = Image.open("test-image-cover.jpeg") | ||
|
||
# Create a new image for the watermark with an alpha layer (RGBA) | ||
# the same size as the original image | ||
watermark = Image.new("RGBA", main.size) | ||
# Get an ImageDraw object so we can draw on the image | ||
waterdraw = ImageDraw.ImageDraw(watermark, "RGBA") | ||
# Place the text at (10, 10) in the upper left corner. Text will be white. | ||
waterdraw.text((100, 100), "My Python Project") | ||
|
||
# Get the watermark image as grayscale and fade the image | ||
# See <http://www.pythonware.com/library/pil/handbook/image.htm#Image.point> | ||
# for information on the point() function | ||
# Note that the second parameter we give to the min function determines | ||
# how faded the image will be. That number is in the range [0, 256], | ||
# where 0 is black and 256 is white. A good value for fading our white | ||
# text is in the range [100, 200]. | ||
watermask = watermark.convert("L").point(lambda x: min(x, 100)) | ||
# Apply this mask to the watermark image, using the alpha filter to | ||
# make it transparent | ||
watermark.putalpha(watermask) | ||
|
||
# Paste the watermark (with alpha layer) onto the original image and save it | ||
main.paste(watermark, None, watermark) | ||
main.save("12volt-watermarked.jpg", "JPEG") | ||
|
||
main() |
Binary file not shown.