I have a collection of one thousand HTML files I need to clean up for printing. I need to delete all the tags inside the <body></body> area except for one, <div.pg>. The excess are navigation links which make the printouts messy and use up paper. The contents of the tags are not the same, so I can't find and replace the code excerpt, but the tags are the same insofar as there are three <table> tags to be deleted, each with specific class. Is there any batch processing technique or software to do this job? I'm using Windows

  • 1
    Write a python script for this. – James T Snell Sep 27 '11 at 20:45
  • What operating system are you using? – bryan Sep 27 '11 at 21:16
  • I'm on Windows XP – z403 Sep 27 '11 at 21:17
  • Are these files you've created or just general pages on the internet? – ChrisF Sep 28 '11 at 20:21
  • Write a Perl script for this. – Randolf Richardson Sep 29 '11 at 10:45

one thousand HTML files … make them clean to be printed.

An easy solution for suppressing sections when printing is to use a CSS stylesheet

Add something like this to the head element

<link rel="stylesheet" 
   media="print" href="print.css" />

Note the media="print" - this stylesheet only applies when printing, not when viewing.

If your HTML is all formatted in a similar way, you might do this for thousands of html files in a single command with a simplistic pattern matching edit

perl -i -ne "print; print '<link … />' if /<head>/" dir1/*.html dir2/*.html

In print.css, set display:none for elements (e.g. whole divs) you don't want printed. For example

#menu { display: none; }

See A List Apart article

Update: If your thousand html files are already using a common CSS stylesheet file, the solution is even easier, no need to change or add anything in the html files, just add a section to the existing stylesheet to handle printing. For example:

@media screen
  #menu {font-family:verdana,sans-serif;font-size:14px;}
@media print
  #menu {display:none;}
  • 1
    Yeah, the only way to solve this is with any scripting language or tool (like sed.exe or awk.exe) supporting real regular expressions OR with a XML library such as a Beanshell script running XMLUNIT . – djangofan Sep 27 '11 at 22:17
  • 3
    +1 for solving the real problem, not giving them what they think is the solution! – Arjang Sep 27 '11 at 22:19

Use Notepad++. You're able to do a find/replace text across multiple files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.