Debate template and color separation script
答辩模板
在华师大官网上有相关的答辩模板可使用:https://www.ecnu.edu.cn/wzcd/xxgk/xxbs.htm。
如果使用作者改进后的模板文件,在这里下载修改后华东师范大学模板。
文件打印
打印如果需要区分彩色和黑白页以节约成本,可尝试使用本文中的脚本完成。
用法:
- Install gs and pdftk
- pdftk: https://www.pdflabs.com/tools/pdftk-server/
- gs: https://ghostscript.com/releases/gsdnld.html (64-bit+GNU Affero General Public License)
- Run: python this_script_name.py colorfy_document.pdf
- BWs ends with
_bwsplit.pdf
, while colors ends with_colorsplit.pdf
完整参数:
this_script_name.py [OPTIONS] colorfy_document.pdf
Options:
- -m Write out the file in multiple parts rather than a PDF for each different section;
- -s option chooses simplex rather than duplex output;
- -v verbose.
Python文件:
#!/usr/bin/env python
# Install gs and pdftk
# Run: python this_script_name.py colorfy_document.pdf
# BWs ends with `_bwsplit.pdf`, while colors ends with `_colorsplit.pdf`
import fetch_num
import os, os.path, sys, string, re, tempfile, shutil, getopt
threshold=20 # Recommended number, to define the distance between R and G / R and B for each pixel.
def a2b(x):
"""Turn ascii into bytes for Python 3, in way that works with Python 2"""
try:
return bytes(x)
except:
return bytes(x, 'ascii')
def iscolorppm(filename):
"""Does the PPM file contain any non-grayscale colors?"""
file = open(filename, 'rb')
# Ugly: I read the whole file into RAM, and copy it needlessly a lot
data = file.read()
file.close()
# PPM is a *very* liberal file format. It allows comments anywhere in the
# header, even in the middle of tokens.
comments_re = re.compile(a2b('^([^ \t\n]*)#[^\n]*\n'))
split_re = re.compile(a2b('^([ \t\n]|#[^\n]*\n)+([^ \t\n#])'))
tok_re = re.compile(a2b('^([^ \t\n]*)([ \t\n].*)'), re.DOTALL)
toks = []
while len(toks) < 4:
while split_re.match(data):
data = split_re.sub(r'\2', data)
while comments_re.match(data):
data = comments_re.sub(r'\1', data)
(tok, data) = tok_re.match(data).groups()
toks.append(tok)
magic = toks[0]
(width, height, max_color) = map(int, toks[1:])
data = data[1:]
if magic == b'P3':
binary = False
elif magic == b'P6':
binary = True
else:
print("%s is not a valid PPM file" % filename)
sys.exit(1)
# Massage data so adjacent triples should have the same value in b/w images
data_len = width*height*3
if binary:
if int(max_color) > 255:
# Untested. Each intensity is in two bytes.
data_len *= 2
data = data[1:data_len:2] + data[:data_len:2]
else:
data = [int(x) for x in data.split()]
if len(data) < data_len:
print('PPM file is truncated?')
sys.exit(1)
triples = zip(data[0:data_len:3], data[1:data_len:3], data[2:data_len:3])
black_and_white = all((abs(a-b)<threshold and abs(a-c)<threshold for (a,b,c) in triples))
return not black_and_white
def pdfcolorsplit(file, doublesided, merge, verbose):
# Convert to PPMs
if verbose:
print('Analyzing %s...' % file)
tmpdir = tempfile.mkdtemp(prefix = 'pdfcs_')
gs_opts = '-sDEVICE=ppmraw -dBATCH -dNOPAUSE -dSAFE -r20'
if not verbose:
gs_opts += ' -q'
os.system('GSWIN64C ' + gs_opts + ' -sOutputFile="%s" "%s"' \
% (os.path.join(tmpdir, 'tmp%06d.ppm'), file))
PPMs = os.listdir(tmpdir)
PPMs.sort()
PPMpath = [os.path.join(tmpdir, x) for x in PPMs]
# Judge colors
total_pages = len(PPMs)
logical_numbers=fetch_num.getPdfLogicalNumbers(file,total_pages)
iscolor = [iscolorppm(x) for x in PPMpath]
if doublesided:
# Treat as color those b/w pages that share a sheet with a color page
iscolorpair = [x or y for (x,y) in zip(iscolor[::2], iscolor[1::2])]
iscolor[:2*len(iscolorpair):2] = iscolorpair
iscolor[1::2] = iscolorpair
shutil.rmtree(tmpdir)
# Construct page range strings
flips = [x for x in range(2,total_pages+1) if iscolor[x-1] != iscolor[x-2]]
if not flips:
if verbose:
print('No splitting needs to be done, skipping %s' % file)
return
edges = [1] + flips + [total_pages+1]
ranges = ['%d-%d' % (x,y-1) for (x,y) in zip(edges[:-1], edges[1:])]
# Finally output split files
if verbose:
print('Outputing splits as new pdf files...')
base_name = file
if base_name.lower().endswith('.pdf'):
base_name = base_name[:-4]
suffixes = ['_bwsplit.pdf', '_colorsplit.pdf']
# jobs is a seq of (range, filename) pairs, e.g. ('1-3', 'colorbits.pdf')
if merge:
jobs = ((' '.join(ranges[0::2]), base_name + suffixes[iscolor[0]]),\
(' '.join(ranges[1::2]), base_name + suffixes[not iscolor[0]]))
else:
jobs = [(r, '%s_%03d%s' % (base_name,n+1,suffixes[(n+iscolor[0])%2])) \
for (n,r) in enumerate(ranges)]
for (pages, name) in jobs:
if verbose:
print('pdftk "%s" cat %s output "%s"' % (file, pages, name))
os.system('pdftk "%s" cat %s output "%s"' % (file, pages, name))
def usage():
progname = os.path.basename(sys.argv[0])
print('Usage: %s [OPTIONS] <PDF-file(s)>' % progname)
print('')
print('Splits PDF files into color and black and white sections.')
print('')
print('Options:')
print(' -m Write out the file in multiple parts rather than a PDF for')
print(' each different section')
print(' -s option chooses simplex rather than duplex output')
print(' -v verbose.')
def main():
try:
opt_pairs, filenames = getopt.gnu_getopt(sys.argv[1:], "hvpms", ["help"])
except getopt.GetoptError as err:
print(str(err))
usage()
sys.exit(1)
if opt_pairs:
opts = list(zip(*opt_pairs))[0]
else:
opts = []
if ('-h' in opts) or ('--help' in opts) or (not filenames):
usage()
sys.exit()
verbose = '-v' in opts
use_pdftoppm = '-p' in opts
merge = '-m' not in opts
doublesided = '-s' not in opts
for file in filenames:
pdfcolorsplit(file, doublesided, merge, verbose)
if __name__ == "__main__":
main()