
mac命令行將輸出寫入文件

There are various reasons why you might want to convert a PDF file to editable text. Maybe you need to revise an old document and all you have is the PDF version of it. Converting PDF files in Windows is easy, but what if you’re using Linux?
出于多種原因,您可能希望將PDF文件轉換為可編輯的文本。 也許您需要修改一個舊文檔,而您所擁有的只是它的PDF版本。 在Windows中轉換PDF文件很容易,但是如果使用Linux,該怎么辦?
No worries. We’ll show you how to easily convert PDF files to editable text using a command line tool called pdftotext, that is part of the “poppler-utils” package. This tool may already be installed. To check if pdftotext is installed on your system, press “Ctrl + Alt + T” to open a terminal window. Type the following command at the prompt and press “Enter”.
別擔心。 我們將向您展示如何使用名為pdftotext的命令行工具輕松地將PDF文件轉換為可編輯文本,該工具是“ poppler-utils”軟件包的一部分。 該工具可能已經安裝。 要檢查系統上是否安裝了pdftotext,請按“ Ctrl + Alt + T”打開終端窗口。 在提示符下鍵入以下命令,然后按“ Enter”。
dpkg –s poppler-utils
dpkg –s poppler-utils
NOTE: When we say to type something in this article and there are quotes around the text, DO NOT type the quotes, unless we specify otherwise.
注意:當我們說要在本文中鍵入某些內容并且文本周圍有引號時,請不要鍵入引號,除非我們另外指定。

If pdftotext is not installed, type the following command at the prompt and press “Enter”.
如果未安裝pdftotext,則在提示符下鍵入以下命令,然后按“ Enter”。
sudo apt-get install poppler-utils
須藤apt-get install poppler-utils
Type your password when prompted and press “Enter”.
出現提示時輸入密碼,然后按“ Enter”。

There are several tools available in the poppler-utils package for converting PDF to different formats, manipulating PDF files, and extracting information from files.
poppler-utils軟件包中提供了幾種工具,可用于將PDF轉換為不同格式,處理PDF文件以及從文件中提取信息。

The following is the basic command for converting a PDF file to an editable text file. Press “Ctrl + Alt + T” to open a Terminal window, type the command at the prompt, and press “Enter”.
以下是將PDF文件轉換為可編輯文本文件的基本命令。 按“ Ctrl + Alt + T”打開“終端”窗口,在提示符下鍵入命令,然后按“ Enter”。
pdftotext /home/lori/Documents/Sample.pdf /home/lori/Documents/Sample.txt
pdftotext /home/lori/Documents/Sample.pdf /home/lori/Documents/Sample.txt
Change the path to each file to correspond to the location and name of your original PDF file and where you want to save the resulting text file. Also, change the filenames to correspond to the names of your files.
更改每個文件的路徑,使其與原始PDF文件的位置和名稱以及要保存結果文本文件的位置相對應。 另外,更改文件名以對應于文件名。

The text file is created and can be opened just as you would open any other text file in Linux.
該文本文件已創建并可以打開,就像在Linux中打開任何其他文本文件一樣。

The converted text may have line breaks in places you don’t want. Line breaks are inserted after every line of text in the PDF file.
轉換后的文本可能在您不想要的地方有換行符。 在PDF文件中的每一行文本之后都插入了換行符。

You can preserve the layout of your document (headers, footers, paging, etc.) from the original PDF file in the converted text file using the “-layout” flag.
您可以使用“ -layout”標志保留轉換后的文本文件中原始PDF文件的文檔布局(頁眉,頁腳,頁面等)。
pdftotext -layout /home/lori/Documents/Sample.pdf /home/lori/Documents/Sample.txt
pdftotext -layout /home/lori/Documents/Sample.pdf /home/lori/Documents/Sample.txt

If you want to only convert a range of pages in a PDF file, use the “-f” and “-l” (a lowercase “L”) flags to specify the first and last pages in the range you want to convert.
如果只想轉換PDF文件中的頁面范圍,請使用“ -f”和“ -l”(小寫的“ L”)標志來指定要轉換范圍的第一頁和最后一頁。
pdftotext -f 5 -l 9 /home/lori/Documents/Sample.pdf /home/lori/Documents/Sample.txt
pdftotext -f 5 -l 9 /home/lori/Documents/Sample.pdf /home/lori/Documents/Sample.txt

To convert a PDF file that’s protected and encrypted with an owner password, use the “-opw” flag (the first character in the flag is a lowercase letter “O”, not a zero).
要轉換使用所有者密碼保護和加密的PDF文件,請使用“ -opw”標志(標志中的第一個字符是小寫字母“ O”,而不是零)。
pdftotext -opw ‘password’ /home/lori/Documents/Sample.pdf /home/lori/Documents/Sample.txt
pdftotext -opw'password'/home/lori/Documents/Sample.pdf /home/lori/Documents/Sample.txt
Change “password” to the one used to protect the original PDF file being converted. Make sure there are single quotes, not double, around “password”.
將“密碼”更改為用于保護要轉換的原始PDF文件的密碼。 確保在“密碼”周圍有單引號而不是雙引號。

If the PDF file is protected and encrypted with a user password, use the “-upw” flag instead of the “-opw” flag. The rest of the command is the same.
如果PDF文件受用戶密碼保護和加密,請使用“ -upw”標志而不是“ -opw”標志。 其余命令相同。

You can also specify the type of end-of-line character that is applied to the converted text. This is especially useful if you plan to access the file on a different operating system like Windows or Mac. To do this, use the “-eol” flag (the middle character in the flag is a lowercase letter “O”, not a zero) followed by a space and the type of end-of-line character you want to use (“unix”, “dos”, or “mac”).
您還可以指定應用于轉換后的文本的行尾字符的類型。 如果打算在Windows或Mac等其他操作系統上訪問文件,則此功能特別有用。 為此,請使用“ -eol”標志(標志的中間字符是小寫字母“ O”,而不是零),后跟一個空格和要使用的行尾字符類型(“ unix”,“ dos”或“ mac”)。

NOTE: If you don’t specify a filename for the text file, pdftotext automatically uses the base of the PDF filename and adds the “.txt” extension. For example, “file.pdf” will be converted to “file.txt”. If the text file is specified as “-“, the converted text is sent to stdout, which means the text is displayed in the Terminal window and not saved to a file.
注意:如果未為文本文件指定文件名,則pdftotext會自動使用PDF文件名的基礎并添加“ .txt”擴展名。 例如,“ file.pdf”將被轉換為“ file.txt”。 如果文本文件指定為“-”,則轉換后的文本將發送到stdout,這意味著該文本顯示在“終端”窗口中,而不保存到文件中。
To close the Terminal window, click the “X” button in the upper-left corner.
要關閉終端窗口,請單擊左上角的“ X”按鈕。
For more information about the pdftotext command, type “man page pdftotext” at the prompt in a Terminal window.
有關pdftotext命令的更多信息,請在“終端”窗口的提示符下鍵入“手冊頁pdftotext”。
翻譯自: https://www.howtogeek.com/228531/how-to-convert-a-pdf-file-to-editable-text-using-the-command-line-in-linux/
mac命令行將輸出寫入文件