Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
1  
Write a python script for this. –  Doc 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

2 Answers 2

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" 
   type="text/css"
   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;}
  }
share|improve this answer
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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