[PYTHON] I want to copy an English paper from pdf and put it in Google Translate

1. Overview

This is used when you want to copy and paste a paper from pdf and translate it into Google. Open the output conv.html and right-click to translate. I needed it in a hurry, so I made it insanely suitable. There are no plans for maintenance.

2. Source code

"""
This is used when you want to copy and paste a paper from pdf and translate it into Google.
.Given the path of txt_conv.Since it will be output in html, open it in chrome and right-click to translate.

・ What you are doing
ctrl in pdf+If you paste it with A, line breaks will be wasted and it will not translate well.
Avoid it.
"""

import sys
import os

filepath = sys.argv[1]

# ---Process text for Google Translate
with open(filepath, "r", encoding="utf-8") as f:

    lines = f.readlines()
    newlines = []
    lenbuf = []  #line len buffer
    for line in lines:
        if len(line) >= 2:
            if line[-2] == ".":  #Insert a line break if the end is a period
                line = line[:-1] + "<br>"
            else:  #If not, put a space
                line = line[:-1] + " "
        newlines.append(line)
        lenbuf.append(len(line))
f.close()

# ---If it is better to insert a line break, insert a line break(average line length*0.Set 8 as the threshold)
ave_len = sum(lenbuf) / len(lenbuf)
for n, linelen in enumerate(lenbuf):
    if linelen < ave_len * 0.8:
        newlines[n] = newlines[n] + "<br>"

# ---_Save with conv
savepath = os.path.splitext(filepath)[0] + "_conv.html"

with open(savepath, "w", encoding="utf-8") as f:
    f.writelines(newlines)

Recommended Posts

I want to copy an English paper from pdf and put it in Google Translate
If you want to put an argument in the closure function and execute it later
I want to create a pipfile and reflect it in docker
I want to pass an argument to a python function and execute it from PHP on a web server
I want to write an element to a file with numpy and check it.
I tried using Google Translate from Python and it was just too easy
I want to replace the variables in the python template file and mass-produce it in another file.
I want to get rid of import warnings from Pyright and pylint in VS Code
I want to improve efficiency with Python even in an experimental system (2) RS232C and pySerial
I want to count unique values in arrays and tuples
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to copy yolo annotations
I made a POST script to create an issue on Github and register it in the Project
Output the report as PDF from DB with Python and automatically attach it to an email and send it
I put Alpine Linux in Larkbox and made it my home server
I want to operate DB using Django's ORM from an external application
Tips for manipulating numpy.ndarray from c ++ -I want to use an iterator-
I want to convert a table converted to PDF in Python back to CSV
I want to color a part of an Excel string in Python
[Google Colab] I want to display multiple images side by side in tiles
I want to leave an arbitrary command in the command history of Shell
For the first time in Numpy, I will update it from time to time
Implemented in Dataflow to copy the hierarchy from Google Drive to Google Cloud Storage
I want to use jar from python
I want to make an automation program!
I want to embed Matplotlib in PySimpleGUI
I want to visualize where and how many people are in the factory
[Python] I want to know the variables in the function when an error occurs!
I made an image classification model and tried to move it on mobile
I want to store the result of% time, %% time, etc. in an object (variable)
I want to convert horizontal text to vertical text and post it on Twitter etc.
I want to get the file name, line number, and function name in Python 3.4
I want to do Dunnett's test in Python
I want to pin Datetime.now in Django tests
I want to create a window in Python
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to perform SageMaker inference from PHP
I want to be an OREMO with setParam!
I want to store DB information in list
I want to merge nested dicts in Python
I want to make fits from my head
I want to use ceres solver from python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I want to display the progress in Python!
What I did when I was angry to put it in with the enable-shared option
I want to write a triple loop and conditional branch in one line in python
I want to get an error message in Japanese with django Password Change Form
I want to divide an nth-order Bezier curve at an arbitrary point ~ Recursion and matrix ~
Memorandum (in openpyxl ① copy and paste from another book ② refer to the comparison table)
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~