0
votes

I have created a Latex template and wanted to use string placeholder to generate forms.

templateB = open('C:/Users/ustjo/Desktop/MM/HSF/HSF-Forms-Generation/template/form-b.tex', 'r').read()

templateB

"\documentclass[11pt]{book}\n\n\input{preamble.tex}\n\n\begin{document}\n\n\nForm B\n\n\section{Expansion of Hong Kong International Airport into a Three-Runway System}\n\nMarine Travel Routes and Management Plan for High Speed Ferries of SkyPier\n\n\subsection{\ul{Case Audit and Checking Record}}\n\n{\renewcommand{\arraystretch}{1.4}\n\begin{table}[htb]\n\fontsize{11pt}{15pt}\selectfont\n\begin{tabular}{|>{\raggedright}p{38mm}|\np{99mm}<{\raggedright}|}\hline\n%%%\nReference Plan: & Marine Travel Routes and Management Plan for High Speed Ferries of SkyPier (The Plan) (EP Condition 2.10)\n\\ \hline\nMonitoring Data: & Ferry movement data collected in the period between\n\newline\n\ul{%(Period)s}\n\\[1.0mm]\hline\nInformation and Data Checked: &\n\begin{minipage}[t]{110mm}\n\begin{itemize}\n\item Automatic Identification System (AIS)\n\item Data Daily SkyPier HSF movements\n \item Record of potential deviations\n\item Response provided by the ferry operators\n\end{itemize}\vspace*{-1.5ex}\n\end{minipage}\n\\\hline\nCase No: & \textit{%(Case No)s}\n\\ \hline\nDate: & \textit{%(Date)s}\n\\ \hline\nFerry Details: & Ferry Number: %(Ferry No)s\n\newline Average Speed: %(Avg Speed)s knots\newline Range of Instantaneous Speed: %(Min Speed)s -- %(Max Speed)s knots\newline Duration of Instantaneous Speeding: %(Duration)s\n\\ \hline\nComments and Observations \newline From ET: & %(Comments)s \\ \hline\nComments and Observations\newline From IEC Marine Advisor: &\n\\ \hline\nReason(s) valid\\ according to \\ The Plan? &\n\begin{minipage}[t]{95mm}\n\begin{itemize}\n\item Yes (case closed)\n\item[\nocheck] No (The ET to confirm with AA MCDD on the required follow up actions)\n\end{itemize}\vspace*{-1.5ex}\n\end{minipage}\n\\\hline\n\end{tabular}\n\end{table}\n}\n\n\n{\fontsize{10pt}{12pt}\selectfont\n\begin{tabular}{:L{23mm}:L{35mm}:L{35mm}:L{35mm}:}\n\hdashline\n\Gape[14pt]{}& \makecell[l]{ET Leader /\\[2mm]\nET's Representative} &\n\makecell[l]{IEC/ \\[2mm]\nIEC's Representative} &\n\makecell[l]{PM /\\[2mm]\nPM's Representative} \\ \hline\nSignature &\includegraphics[scale=1.0]{sig1.pdf}\n& & \\ \hdashline\nName & Terence Kong & &\n\Gape[10pt]{}\n\\\hdashline\n\end{tabular}\n}\n\n\n\end{document}\n"

When I try to run the template, using a loop

page = templateB%{'Period':formB.iloc[i,8],'Case No':formB.iloc[i,1], 'Date':formB.iloc[i,2], 
                      'Ferry No':formB.iloc[i,3], 'Avg Speed':formB.iloc[i,4],'Min Speed':formB.iloc[i,5], 
                      'Max Speed':formB.iloc[i,6], 'Duration':formB.iloc[i,7], 'Comments':formB.iloc[i,9]}

It returns

ValueError                                Traceback (most recent call last)
<ipython-input-7-afe0b30f82bd> in <module>()
      2     page = templateB%{'Period':formB.iloc[i,8],'Case No':formB.iloc[i,1], 'Date':formB.iloc[i,2], 
      3                       'Ferry No':formB.iloc[i,3], 'Avg Speed':formB.iloc[i,4],'Min Speed':formB.iloc[i,5],
----> 4                       'Max Speed':formB.iloc[i,6], 'Duration':formB.iloc[i,7], 'Comments':formB.iloc[i,9]}
      5 
      6     file_name_tex = str(formB.iloc[i,0]) + '.tex'

ValueError: unsupported format character '?' (0xa) at index 448

I only got one '?' in the latex. And I tried to delete that '?'. The problem still exists.

Can anyone help?

1
Are you sure? 0xa is line feed, ? is 0x3f. The error message seems wrong.mhawke
I copy and paste the error.JOHN
I have added the complete error msg to the questionJOHN
From where are you getting the latex document? Please show the code that sets templateB.mhawke
Please check the edit.JOHN

1 Answers

3
votes

Escape the literal % characters that are not part of a substitution format specifier. Some occur on line 21:

%%%

Escape each % with another % character, so the line would be:

%%%%%%

You must do this for all literal % characters in your document.