verbreed / prototype-assist

Script to help with administrative tasks for building prototypes for physical games. This currently includes making numerous deck images for TableTop Simulator (TTS), some board making code specific to my purposes, and making the instruction manual (currently in production)
MIT License
0 stars 0 forks source link

implement text to images functionality #25

Closed bcvilnrotter closed 1 year ago

bcvilnrotter commented 1 year ago

This functionality has become important for creating images for physical game manuals.

Would like to keep dependencies as low as possible, but may need to consider either a google API for google docs, or some native module for pulling text and formatting from Microsoft Word.

bcvilnrotter commented 1 year ago

for Google Docs API, there is promise in accessing documents using the below guide

https://developers.google.com/docs/api/quickstart/python

bcvilnrotter commented 1 year ago

Current error when looking at local files appears to be related to the documents on the google drive being in .docx form

<HttpError 400 when requesting #### returned "This operation is not supported for this document". Details: "This operation is not supported for this document">

will need to find a way around this later.

bcvilnrotter commented 1 year ago
Here is an example of how you can program a Python script to accomplish this task:

Use the python-docx library to extract the text and formatting information from the .docx file. You can install it using pip install python-docx.

Use the google-auth and google-api-python-client libraries to authenticate and access the Google Drive API. You can install them using pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client.

Use the Google Drive API to download the Google Docs file as a .docx file.

Use the Pillow library to open the image template and paste the text and formatting onto it using the information from step 1. You can install it using pip install Pillow

Use the Pillow library to open images from the .docx file and paste it onto the image template.

Save the final image.

python
Copy code
import docx
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from PIL import Image

def get_doc_text(file_path):
    doc = docx.Document(file_path)
    fullText = []
    for para in doc.paragraphs:
        fullText.append(para.text)
    return '\n'.join(fullText)

def download_file_from_google_drive(file_id, file_name):
    try:
        # create drive api client
        creds = Credentials.from_authorized_user_info(info=None)
        drive_service = build('drive', 'v3', credentials=creds)

        # request data from drive api
        request = drive_service.files().get_media(fileId=file_id)
        fh = io.BytesIO()
        downloader = MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Download %d%%." % int(status.progress() * 100))

        # save file data to disk
        fh.seek(0)
        with open(file_name, 'wb') as f:
            f.write(fh.read())

    except HttpError as error:
        print(F'An error occurred: {error}')
        return None
    return file_name

# Assume that you have the image template in the path
template_path = 'template.jpg'
template = Image.open(template_path)

# Assume that you have the google doc file id and name
file_id = '1abcdefghijklmnopqrstuvwxyz'
file_name = 'my_doc.docx'

# Download the file from google drive
downloaded_file = download_file_from_google_drive(file_id, file_name)

# Get the text and formatting information from the docx file
text = get_doc_text(downloaded_file)

# paste the text and images onto the image template using PIL library

#save the --

# paste the text and images onto the image template using PIL library
# you can use the text and formatting information obtained from the previous step
# to paste the text onto the template

# open images from the docx file and paste it onto the template
# you can use the python-docx library to get a list of the images in the docx file
# and then open each image using the PIL library

# save the final image
template.save('final_image.jpg')
bcvilnrotter commented 1 year ago

With the merging of PR #34 we have untested code that should be able to pull down a file object from google drive as long as the user gives a path on their computer to the credentials.json files they must get from google for this to work.

We should be able to begin working on actually taking text from the object, and putting it on a number of template images...and then a LOT more coding...

bcvilnrotter commented 1 year ago

here is another website for reference.

bcvilnrotter commented 1 year ago

Closing this. I'm no longer fond of issues that just track the number of issues in a milestone. The milestone label should do this. Closing this as complete.