{"id":15853,"date":"2023-12-12T09:06:49","date_gmt":"2023-12-12T16:06:49","guid":{"rendered":"https:\/\/macblaze.ca\/?p=15853"},"modified":"2023-12-12T15:39:40","modified_gmt":"2023-12-12T22:39:40","slug":"__trashed-2","status":"publish","type":"post","link":"https:\/\/macblaze.ca\/?p=15853","title":{"rendered":"Python in Ebook Production"},"content":{"rendered":"<p>The following is a blog post I originally wrote on behalf of <a href=\"https:\/\/www.orcabook.com\/\">Orca Book Publishers<\/a> for the APLN (Accessible Publishing Learning Network) <a href=\"https:\/\/apln.ca\/category\/blog\/\">website<\/a>. I had done a brief online Q&amp;A on behalf of eBound talking about our Benetech certification and there were questions about my python workflow. So I tried to write it out, which was a good exercise in and of itself.<\/p>\n<hr \/>\n<p><strong>In 2022 Orca Book Publishers<\/strong> had a dozen accessible titles that had been remediated via programs with BooksBC and eBound as well as another group that had been created as mostly accessible epubs by outsourced ebook developers. When Orca made a commitment to creating accessibility ebooks the immediate goal was to pursue Benetech Certification with an eye to adopting a born accessible workflow and to start remediating backlist titles.<\/p>\n<p>Orca has three main streams of books from an epub point of view: highly illustrated non-fiction, fiction with few or no images, and picture books. We started by remediating the fiction titles that were already mostly accessible and bringing them up to Benetech standards.<\/p>\n<p>Concurrently we brought the non-fiction production in-house to begin to develop a functional accessible workflow. Non-fiction titles usually feature 80 plus illustrations and photographs, multiple sidebars, a glossary, index, and a bibliography.<\/p>\n<p>In publishing circles a fair amount of time is spent bemoaning the shortcomings of InDesign as a platform for creating good epubs, let alone making accessible ones. With a complex design, you can spend a lot of time and effort prepping an InDesign file to export a &#8220;well-formed&#8221; file and still end up with a \u201cmessy\u201d end result. Instead, Orca\u2019s approach was to ignore InDesign as much as possible, export the bare necessities (styles, ToC, page markers etc.), clean out the junk in the epub it produces using a series of scripted search and replaces, and then rely on post-processing to produce well-formed, accessible epub in a more efficient manner.<\/p>\n<p>To that end we started building two things: a comprehensive standard structure and its accompanying documentation for an Orca ebook, and a series of python scripts to apply that structure to epubs. These scripts needed to be robust enough to work with both new books and to remediate older titles that spanned everything from old epub2\u2019s to mostly-accessible titles that didn\u2019t quite meet Benetech standards.<\/p>\n<h2>Python in Epub production<\/h2>\n<p>Python was the obvious choice for these tasks. Python is a programming language suited for text and data manipulation that is highly extensible, with thousands of external libraries available, and has a focus on readability. It comes already installed with Mac OSX and is easily added to both Windows and Linux.<\/p>\n<p>Python is easy to learn and fairly easy to use.\u00a0 You can simply write a python script in a text file e.g.:<\/p>\n<p><code><span class=\"pythonkeywordcolor\">print<\/span>(<span class=\"pythonstringcolor\">'Enter your name:'<\/span>)<\/code><br \/>\n<code>name = <span class=\"pythonkeywordcolor\">input<\/span>()<\/code><br \/>\n<code><span class=\"pythonkeywordcolor\">print<\/span>(<span class=\"pythonstringcolor\">'Hello, '<\/span> + name)<\/code><\/p>\n<p>Then save it as script.py and run it using a python interpreter. As a general rule writing and running python scripts from within an IDE (integrated development environment) like <a href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noopener\">Visual Studio<\/a><a href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noopener\"> Code<\/a>, a free IDE\u00a0 created and maintained by Microsoft, makes this pretty simple. Using VS Code allows a developer to easily modify scripts and then run them from within the same application.<\/p>\n<h3>Regular Expressions<\/h3>\n<p>The other important part of the process and well worth learning as much as they can about \u2014 even if they don\u2019t dive into python \u2014 is regular expressions (regex). This a system of patterns of that allow you to search and replace highly complex strings.<\/p>\n<p>For instance if you wanted to replace all the <code>&lt;p&gt;<\/code>\u2019s in a glossary with <code>&lt;li&gt;<\/code>\u2019s:<\/p>\n<p><code>&lt;p class=\"glossary\"&gt;&lt;b&gt;regular Expression&lt;\/b&gt;: is a sequence of characters that specifies a match pattern in text.&lt;\/p&gt;<\/code><\/p>\n<p>You could search for:<\/p>\n<p><code>&lt;p class=\"glossary\"&gt;(.*?)&lt;\/p&gt;<\/code><\/p>\n<p>where the bits in parentheses are wildcards&#8230;and replace it with:<\/p>\n<p><code>&lt;li class=\"glossary\"&gt;\\1&lt;\/li&gt;<\/code>.<\/p>\n<p>For each occurrence found, the bit in the parentheses would be stored and then reinserted correctly in the new string.<\/p>\n<p>Once you start to use regexes you\u2019ll quickly get addicted to the power and flexibility and quite a few text editors (even InDesign via grep) support regular expressions.<\/p>\n<h3>Scripting Python<\/h3>\n<p>With these two tools you can write a fairly basic script that opens a folder (an uncompressed epub) and loops through to find a file named glossary.xhtml and replace the <code>&lt;p class=\"glossary\"&gt;<\/code> tag and replace it with a <code>&lt;li&gt;<\/code> \u2014 or whatever else you might need. You can add more regexes to change the <code>&lt;title&gt;<\/code> to <code>&lt;title&gt;Glossary&lt;\/title&gt;<\/code>, add in the proper section epub:type\u2019s and roles and more. Since InDesign tends to export fairly regular epub code once you clean out the junk, if you create a standard set of styles, it means you can easily clean and revise the whole file in a few key strokes.<\/p>\n<p>Taking that one step further, if you ensure that the individual files in an epub are named according to their function e.g about-the-author.xhtml, copyright.xhtml, dedication.xhtml etc. you can easily have custom lists of search\/replaces that are specific to each file, ensuring things like applying epub:types and aria-roles is done automatically or you could edit or change existing text with new standardized text in things like the .opf file.<\/p>\n<p>If you build basic functions to perform search and replaces, then you can continually update and revise the list of things you want it to fix as you discover both InDesign and your designer&#8217;s quirks, things like moving spaces outside of spans or restructuring the headers. If you can conceptualize what you want to do, you can build a regex to do it and just add it to the list.<\/p>\n<p>You can also build multiple scripts for different stages of the process or expand into automating other common tasks. For instance the Orca toolset currently has the following scripts:<\/p>\n<ul>\n<li>clean_indesign (cleans all the crud out and tidies up some basic structures),<\/li>\n<li>clean_epub (which replaces all the headers, adds a digital rights file, rewrites the opf file to our standard, standardizes the ToC and landmarks and more&#8230;),<\/li>\n<li>alt-text-extract (extracts image names, alt text and figcaptions to an excel spreadsheet),<\/li>\n<li>update_alt text (loads an excel spreadsheet that has alt text and, based on image file names, inserts it into the proper <code>&lt;img alt=\"\"<\/code>),<\/li>\n<li>run_glossary (which searches the glossary.xhtml and creates links to the glossed words in the text),<\/li>\n<li>extract_metadata (which loops through all the epubs in a folder and pulls the specified metadata e.g. pub date, modified date, a11y metadata, rights etc.),<\/li>\n<li>extract_cover _alt (loops through a folder of epubs and extracts the cover alt text into a excel spreadsheet),<\/li>\n<li>increment_pagenumber (some of our older epubs were made from pdfs that had different page numbering from the printed book, so this script goes through and bumps them by a specified increment)<\/li>\n<\/ul>\n<p>You can see the InDesign cleaning script here: <a href=\"https:\/\/github.com\/b-t-k\/epub-python-scripts\">github.com\/b-t-k\/epub-python-scripts<\/a> as a basic example. As we continue to clean up and modify the rest they will slowly be added to the repository.<\/p>\n<div>So you can see, learning and using python in your workflow can speed up a lot of repetitive and time consuming tasks and actually ensure a better quality and more standardized book\u2014 which incidentally means making future changes to epubs becomes much more efficient.<\/div>\n<h3>Documentation<\/h3>\n<p>Concurrently to all this Orca maintains and continually revises a set of documents that records all the code and standards we have decided on. It is kept in a series of text files that automatically update a local website and it contains everything from the css solutions we use to specific lists of how ToC\u2019s are presented, our standard schema, how we deal with long descriptions, lists of epub-types and aria roles and a record of pretty much any decision that is made regarding how Orca builds epubs. Because website is searchable, a quick search easily finds the answer to most questions.<\/p>\n<h2>Our Books<\/h2>\n<p>This type of automation has allowed us to produce accessible non-fiction titles in-house and in a reasonable time framework. Books like <a href=\"https:\/\/www.orcabook.com\/Open-Science\" target=\"_blank\" rel=\"noopener\">Open Science<\/a> or <a href=\"https:\/\/www.orcabook.com\/Get-Out-and-Vote\" target=\"_blank\" rel=\"noopener\">Get Out and Vote!<\/a> can be produced in a Benetech certifiable epub in just a few days even though they feature things like indexes, linked glossaries, long descriptions for charts and a lot of alt text that was written after the fact.<\/p>\n<div>And if we have the alt text ready (which is now starting to happen as a part of the workflow), producing non-fiction titles will usually take less than two days. This time frame does grow if we are remediating old epubs that were produced out-of-house and we are giving serious thought to going back and redoing them as it might be quicker. Also with the establishment of the new workflow, remediating fiction titles (or producing them from scratch) now just takes a couple of hours! (excluding QA).<\/div>\n<div><\/div>\n<h3>Producing an Accessible epub<\/h3>\n<div>\n<p>Orca\u2019s production process has been continually evolving. We started by focussing on making accessible non-fiction epubs without alt text, and then brought alt text into the mix after about 9 months (two seasons)\u2014the scripts meant it was easy to go back and update those titles after alt text was created. Meanwhile we pursued Benetech certification for our fiction titles that were produced out-of-house and developed a QA process to ensure compliance. And just recently we have brought fiction production in-house as well.<\/p>\n<p>At this point, as soon as the files have been sent to the printer, the InDesign files are handed over to produce the epub. Increasingly before this stage, the alt text is produced and entered in a spreadsheet. Then this is merged into the completed epub. A \u201cfirst draft\u201d is produced and run through Pagina\u2019s EPUBCheck and Ace by DAISY to ensure compliance. Then, along with a fresh export of the alt text in a separate excel file, it is sent over to our production editor who has a checklist of code elements to work through using BBEdit, and then he views the files in Thorium and Apple Books, and occasionally Colibrio\u2019s excellent online <a href=\"https:\/\/demo.colibrio.com\/\" target=\"_blank\" rel=\"noopener\">Vanilla Reader<\/a>, checking styles, hierarchy, visual presentation and listening to the alt text.<\/p>\n<p>Changes come back and usually within one or two rounds it is declared finished and passed on to the distribution pipeline. There our Data Specialist does one last check of the metadata ensuring it matches the onix files and reruns EPUBCheck and ACE before sending it out.<\/p>\n<h3>Spreading the load<\/h3>\n<p>In the background we have marketing and sales staff working on spreadsheets of all our backlist, writing and proofing alt text for the covers and interior illustration of the fiction books so it is ready to go as titles are remediated. The hope is to incorporate this cover alt text into all of our marketing materials and websites as the work is completed.<\/p>\n<p>The editors meanwhile are just starting to incorporate character styles in Word (especially in specifying things like languages and italics vs. emphasis) and working with authors to build in alt text creation alongside the existing caption-writing process.<\/p>\n<p>The designers are slowly incorporating standardized character and paragraph styles into their design files and changing how they structure their documents to facilitate epub exports. They are also working with the illustrators to collect and preserve their illustration notes in order to help capture the intent of illustrations so those notes can be used as a basis for alt text. They are also working to document cover discussion as a way to help facilitate more interesting and accurate cover alt text.<\/p>\n<p>It will take a few more years but eventually the whole process for producing born accessible, reflowable epubs should be fully in place.<\/p>\n<\/div>\n<h2>The Future<\/h2>\n<div>Orca is currently working towards a goal of 300 Benetech accessible epub titles in our catalog for February 2024, including everything back to 2020. And then we will continue to remediate all our backlist of over 1200 titles over the next few years.<\/div>\n<div><\/div>\n<div>As soon as the process for fiction epubs has solidified, we\u2019d also like to start in on our pictures books and ensure that these fixed epubs are as accessible as possible. It is currently an extremely time-consuming task, but we have hope that we can eventually work out a way to automate a lot of the repetitive work.<\/div>\n<div><\/div>\n<div>This means we need to continue to educate ourselves and our suppliers and work towards a way to standardize as many aspects of the workflow as possible. The more standards we create and maintain the more automation we can employ.<\/div>\n<div><\/div>\n<div>And of course, this means learning even more python&#8230;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The following is a blog post I originally wrote on behalf of Orca Book Publishers for the APLN (Accessible Publishing Learning Network) website. I had done a brief online Q&amp;A on behalf of eBound talking about our Benetech certification and there were questions about my python workflow. So I tried to write it out, which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false},"categories":[1,48],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/macblaze.ca\/index.php?rest_route=\/wp\/v2\/posts\/15853"}],"collection":[{"href":"https:\/\/macblaze.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/macblaze.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/macblaze.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/macblaze.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15853"}],"version-history":[{"count":15,"href":"https:\/\/macblaze.ca\/index.php?rest_route=\/wp\/v2\/posts\/15853\/revisions"}],"predecessor-version":[{"id":15874,"href":"https:\/\/macblaze.ca\/index.php?rest_route=\/wp\/v2\/posts\/15853\/revisions\/15874"}],"wp:attachment":[{"href":"https:\/\/macblaze.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/macblaze.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/macblaze.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}