یکی مسائلی که در ویرایش پستهای سایت به تولیدکنندگان محتوا توصیه میکنم، این است که متن را به صورت پاراگراف به پاراگراف به چتجیپیتی بدهند. این کار چند دلیل دارد:
- اولاً، حتی در پلنهای پولی چتجیپیتی، زمانی که حجم داده ورودی و خروجی زیاد میشود، کیفیت خروجی کاهش مییابد.
- دوم اینکه اندازه داده ورودی برای کارکرد عادی در پلن پلاس چتجیپیتی، 4096 کاراکتر است (در زمان نوشتن این مقاله: 9 شهریور 1404). بنابراین، برای مقالات طولانی یا کتابها، عملاً امکان کار با چتجیپیتی وجود ندارد.
- چتجیپیتی این ویژگی را دارد که وقتی خروجی به حد معینی برسد، آن را کوتاه میکند. بنابراین هرچقدر هم از آن بخواهیم که متن پست سایت، مقاله یا کتاب را تغییر ندهد، وقتی محدودیت خروجی را پر کند، خود به خود بخشهایی از متن را تغییر میدهد.
- در متنهای طولانی، تغییرات خودکار بیشتری نسبت به آنچه که ما مدنظر داریم، ایجاد میشود.
- به هر حال، پس از کار با چتجیپیتی روی متن، لازم است که خودمان دوباره متن را ویرایش و بازبینی کنیم.
مجموع این دلایل باعث میشود که بخواهیم متن را به صورت پاراگراف به پاراگراف به چتجیپیتی بدهیم.
برای رفع این مشکل کدی با پایتون نوشتیم که بدون شناسایی ربات بودن بتواند این ارتباط و عملیات را به صورت خودکار در چت جی پی تی ایجاد کند.
این کد یک فرآیند خودکار برای ویرایش متنهای طولانی است. به طور خاص، متنها به صورت پاراگرافی به چتجیپیتی ارسال میشوند و پس از دریافت پاسخ، تغییرات در فایل ورد ذخیره میشود. این روش به ویژه برای متونی که طولانی هستند و نیاز به اصلاحات مکرر دارند، بسیار کاربردی است.
پیشنیازهای Library در سیستم
برای اجرای کدی که در پست قبلی نوشتید، به نصب برخی از ابزارها و بستهها نیاز دارید که میتوانند از طریق خط فرمان (Bash) نصب شوند. این ابزارها شامل selenium، undetected-chromedriver، python-docx و سایر وابستگیهای مرتبط با مرورگر Chrome میباشند. در اینجا به ترتیب دستورات نصب برای راهاندازی محیط مورد نیاز آورده شده است:
1. نصب Python (در صورت نیاز)
اگر Python را نصب نکردهاید، ابتدا باید آن را نصب کنید. برای نصب Python در سیستمهای لینوکسی، دستور زیر را در ترمینال وارد کنید:
sudo apt update
sudo apt install python3 python3-pip
2. نصب Selenium
برای استفاده از Selenium باید پکیج آن را نصب کنید. دستور زیر برای نصب آن است:
pip install selenium
3. نصب undetected-chromedriver
برای استفاده از undetected-chromedriver، که برای جلوگیری از بلاک شدن هنگام استفاده از مرورگر در سایتهای مختلف است، دستور زیر را اجرا کنید:
pip install undetected-chromedriver
4. نصب کتابخانه python-docx
برای خواندن و ویرایش فایلهای DOCX، باید کتابخانه python-docx را نصب کنید:
pip install python-docx
5. نصب ChromeDriver
در ابتدا، برای استفاده از Chrome در Selenium نیاز به نصب ChromeDriver دارید. برای نصب ChromeDriver، مراحل زیر را دنبال کنید:
- دانلود ChromeDriver:
برای دانلود نسخهای که با مرورگر Chrome شما سازگار باشد، به این لینک بروید و نسخه مناسب را دانلود کنید. - نصب ChromeDriver در لینوکس:
پس از دانلود، بایدchromedriverرا به مسیر مناسب در سیستم خود منتقل کنید. معمولاً در لینوکس، دستور زیر برای نصب آن استفاده میشود:sudo mv chromedriver /usr/local/bin/ sudo chmod +x /usr/local/bin/chromedriver
6. نصب Google Chrome
اگر مرورگر Google Chrome را روی سیستم خود نصب ندارید، دستور زیر را برای نصب آن وارد کنید:
sudo apt update
sudo apt install google-chrome-stable
7. اجرا با محیط مجازی (اختیاری)
اگر میخواهید محیطی جداگانه برای پروژه خود ایجاد کنید، استفاده از محیط مجازی (virtual environment) میتواند مفید باشد. دستور زیر برای ساخت یک محیط مجازی و نصب وابستگیها در آن است:
python3 -m venv venv
source venv/bin/activate
سپس میتوانید تمام بستهها را در محیط مجازی نصب کنید.
8. اجرای کد
پس از نصب تمام بستهها و تنظیمات لازم، میتوانید کد خود را با استفاده از دستور زیر اجرا کنید:
python3 your_script.py
با انجام این مراحل، محیط مناسب برای اجرای کد شما آماده خواهد شد.
بخشهای کد پایتون ویرایش متنهای طولانی با پایتون
1. راهاندازی مرورگر و تنظیمات آن:
chrome_options = Options()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--start-maximized")
driver = uc.Chrome(options=chrome_options)
در این بخش، تنظیمات کروم به نحوی انجام میشود که برای استفاده از چتجیپیتی بدون مشکل و موانع احتمالی (مثل محدودیتهای احراز هویت) آماده شود.
2. رفتن به وبسایت چتجیپیتی:
driver.get("https://chat.openai.com/")
sleep(60)
در این قسمت، مرورگر به وبسایت چتجیپیتی هدایت میشود و به کاربر 60 ثانیه فرصت داده میشود که نام کاربری و پسورد خود را وارد کند.
3. بارگذاری فایل ورد و پردازش پاراگرافها:
doc = Document(r"C:\Users\Moein\Desktop\test\test.docx")
for idx, para in enumerate(doc.paragraphs):
if para.text.strip(): # اگر پاراگراف خالی نباشه
print(f"پاراگراف {idx+1}: ارسال به چت جیپیتی -> {para.text}")
در این قسمت، فایل ورد با استفاده از کتابخانه python-docx بارگذاری میشود و به ازای هر پاراگراف موجود در فایل، پردازش انجام میشود.
4. تعامل با چتجیپیتی برای ویرایش متن:
chat_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="prompt-textarea"]'))
)
chat_box.click()
chat_box.send_keys(f"لطفاً این متن را ویرایش کن. چیزی قبل و بعدش نگو فقط متن ویرایش شده رو بده: {para.text}")
chat_box.send_keys(Keys.RETURN)
در این مرحله، برای ارسال هر پاراگراف به چتجیپیتی، منتظر میمانیم تا باکس چت لود شود. سپس، پاراگراف را همراه با درخواست ویرایش به چتجیپیتی ارسال میکنیم.
5. دریافت و ذخیره پاسخ چتجیپیتی:
sleep(10)
responses = driver.find_elements(By.XPATH, '//*[contains(@class, "markdown prose")]')
if responses:
last_response = responses[-1].text.strip()
if last_response:
print(f"متن پاسخ: {last_response}")
para.text = last_response
print(f"پاراگراف {idx+1}: متن در فایل ورد جایگزین شد.")
پس از ارسال درخواست به چتجیپیتی، منتظر میمانیم تا پاسخ آن دریافت شود. سپس، آخرین پاسخ را از وبسایت استخراج کرده و در پاراگراف فعلی فایل ورد جایگزین میکنیم.
6. ذخیره فایل ورد بعد از ویرایش هر پاراگراف:
save_path = r"C:\Users\Moein\Documents\new_test.docx"
doc.save(save_path)
print(f"پاراگراف {idx+1}: فایل ورد ذخیره شد در {save_path}")
پس از ویرایش هر پاراگراف، فایل ورد جدید ذخیره میشود تا تغییرات اعمالشده ذخیره شوند.
7. چاپ محتویات فایل ورد پس از ذخیره:
doc_reloaded = Document(save_path)
for para in doc_reloaded.paragraphs:
print(para.text)
در این بخش، محتویات فایل ورد دوباره خوانده میشود و چاپ میشود تا کاربر مطمئن شود که تغییرات به درستی ذخیره شدهاند.
کد کامل پایتون برای ویرایش متنهای طولانی پست، کتاب و مقاله با پایتون
import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from docx import Document
# تنظیمات کروم
chrome_options = Options()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--start-maximized")
# استفاده از undetected_chromedriver به جای webdriver_manager
driver = uc.Chrome(options=chrome_options)
# رفتن به سایت چت جیپیتی
driver.get("https://chat.openai.com/")
# دادن 1 دقیقه (60 ثانیه) زمان برای وارد کردن نام کاربری و پسورد
print("لطفاً در مدت 60 ثانیه وارد حساب خود شوید...")
sleep(60)
# مسیر فایل ورد
doc = Document(r"C:\Users\Moein\Desktop\test\test.docx")
# برای هر پاراگراف در فایل ورد:
for idx, para in enumerate(doc.paragraphs):
if para.text.strip(): # اگر پاراگراف خالی نباشه
print(f"پاراگراف {idx+1}: ارسال به چت جیپیتی -> {para.text}")
# منتظر ماندن تا باکس چت لود بشه
chat_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="prompt-textarea"]'))
)
# ارسال پاراگراف به ChatGPT برای اصلاح
chat_box.click()
chat_box.send_keys(f"لطفاً این متن را ویرایش کن. چیزی قبل و بعدش نگو فقط متن ویرایش شده رو بده: {para.text}")
chat_box.send_keys(Keys.RETURN)
# منتظر ماندن برای جواب (10 ثانیه)
sleep(10)
# گرفتن متن پاسخ از آخرین پیام
responses = driver.find_elements(By.XPATH, '//*[contains(@class, "markdown prose")]')
if responses:
last_response = responses[-1].text.strip()
if last_response:
print(f"متن پاسخ: {last_response}")
# جایگزینی جواب در پاراگراف ورد
para.text = last_response
print(f"پاراگراف {idx+1}: متن در فایل ورد جایگزین شد.")
# ذخیره فایل ورد بعد از پردازش هر پاراگراف
save_path = r"C:\Users\Moein\Documents\new_test.docx" # مسیر جدید برای ذخیره فایل
doc.save(save_path)
print(f"پاراگراف {idx+1}: فایل ورد ذخیره شد در {save_path}")
# چاپ محتویات فایل ورد برای اطمینان از ذخیرهسازی تغییرات
print("محتویات فایل ورد بعد از ذخیرهسازی:")
doc_reloaded = Document(save_path) # دوباره فایل را باز میکنیم
for para in doc_reloaded.paragraphs:
print(para.text)
Automated Text Editing for Long Posts Using Python and ChatGPT
When editing website posts, I recommend content creators send the text to ChatGPT paragraph by paragraph. There are several reasons for this:
- Output Quality Decreases with Larger Input/Output Sizes: Even with paid plans of ChatGPT, the quality of output decreases as the volume of input and output increases.
- Character Limit in ChatGPT Plus Plan: In the ChatGPT Plus plan, the input data size is limited to 4096 characters (as of September 8, 2025). For long articles or books, it is practically impossible to work with ChatGPT.
- Output Truncation: ChatGPT has a feature where it shortens the output when the limit is reached. Therefore, no matter how much we ask it not to change the text of the post, article, or book, once the output limit is filled, it will automatically modify some parts of the text.
- Automatic Modifications: In long texts, more automatic changes are made than we intend.
In any case, after working with ChatGPT on the text, it is necessary to review and edit the content ourselves again.
All of these reasons lead to the idea of sending the text to ChatGPT paragraph by paragraph.
To solve this problem, we created a Python script that can automatically perform this operation in ChatGPT without being detected as a bot.
Python Code for Editing Long Texts
This code provides an automated process for editing long texts. Specifically, the texts are sent paragraph by paragraph to ChatGPT, and after receiving the response, the changes are saved in a Word file. This method is particularly useful for long texts that need frequent revisions.
Prerequisites
To run the code mentioned in the previous post, you need to install several tools and packages that can be installed via the command line (Bash). These tools include selenium, undetected-chromedriver, python-docx, and other dependencies related to Chrome browser automation. Here are the installation commands to set up the required environment:
- Install Python (if necessary)
If Python is not already installed, you can install it by running the following command:
sudo apt update
sudo apt install python3 python3-pip
- Install Selenium
To use Selenium, you need to install its package:
pip install selenium
- Install Undetected-Chromedriver
To prevent being blocked while using the browser on websites, you need to install undetected-chromedriver:
pip install undetected-chromedriver
- Install Python-Docx Library
To read and edit DOCX files, you need thepython-docxlibrary:
pip install python-docx
- Install ChromeDriver
To use Chrome in Selenium, you need to install ChromeDriver. Download the version that is compatible with your Chrome browser and follow the instructions for installing it on Linux:
sudo mv chromedriver /usr/local/bin/
sudo chmod +x /usr/local/bin/chromedriver
- Install Google Chrome
If you don’t have Google Chrome installed, use the following command to install it:
sudo apt update
sudo apt install google-chrome-stable
- Use Virtual Environment (optional)
If you prefer to create a separate environment for your project, using a virtual environment can be helpful:
python3 -m venv venv
source venv/bin/activate
- Run the Script
After installing all the required packages, you can run the script using the following command:
python3 your_script.py
With these steps, your environment will be ready to execute the code.
Python Code for Editing Long Texts with ChatGPT
Here is the Python code for automatically editing long posts, books, or articles by sending each paragraph to ChatGPT for editing:
1. Browser Setup and Configuration:
chrome_options = Options()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--start-maximized")
driver = uc.Chrome(options=chrome_options)
This section sets up the Chrome browser options to ensure that the script can interact with ChatGPT without encountering authentication barriers or limitations.
2. Navigating to ChatGPT Website:
driver.get("https://chat.openai.com/")
sleep(60) # Wait for the user to log in
The browser is directed to the ChatGPT website, giving the user 60 seconds to log in.
3. Loading the Word File and Processing Paragraphs:
doc = Document(r"C:\Users\Moein\Desktop\test\test.docx")
for idx, para in enumerate(doc.paragraphs):
if para.text.strip(): # Check if paragraph is not empty
print(f"Paragraph {idx+1}: Sending to ChatGPT -> {para.text}")
The Word file is loaded using the python-docx library, and each paragraph is processed one by one.
4. Interacting with ChatGPT for Text Editing:
chat_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="prompt-textarea"]'))
)
chat_box.click()
chat_box.send_keys(f"Please edit this text. Do not add or remove anything, just provide the edited version: {para.text}")
chat_box.send_keys(Keys.RETURN)
For each paragraph, we wait for the ChatGPT chat box to load, send the paragraph, and request it to only edit the text without adding or removing any content.
5. Receiving and Saving ChatGPT’s Response:
sleep(10) # Wait for response
responses = driver.find_elements(By.XPATH, '//*[contains(@class, "markdown prose")]')
if responses:
last_response = responses[-1].text.strip()
if last_response:
print(f"Response Text: {last_response}")
para.text = last_response # Replace paragraph content with the response
After sending the paragraph to ChatGPT, we wait for the response and replace the original paragraph with the edited one.
6. Saving the Edited Word File:
save_path = r"C:\Users\Moein\Documents\new_test.docx"
doc.save(save_path)
print(f"Paragraph {idx+1}: File saved at {save_path}")
Once a paragraph is edited, the file is saved with the changes.Once a paragraph is edited, the file is saved with the changes.
7. Verifying the Saved File:
doc_reloaded = Document(save_path)
for para in doc_reloaded.paragraphs:
print(para.text) # Print the contents of the Word file to verify the changes
finally, the edited document is reloaded, and its contents are printed to ensure that all changes have been correctly applied
import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from docx import Document
# Chrome settings
chrome_options = Options()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--start-maximized")
# Use undetected_chromedriver instead of webdriver_manager
driver = uc.Chrome(options=chrome_options)
# Go to ChatGPT site
driver.get("https://chat.openai.com/")
# Give 1 minute (60 seconds) to enter username and password
print("Please log into your account within 60 seconds...")
sleep(60)
# Path to the Word file
doc = Document(r"C:\Users\Moein\Desktop\test\test.docx")
# For each paragraph in the Word file:
for idx, para in enumerate(doc.paragraphs):
if para.text.strip(): # If the paragraph is not empty
print(f"Paragraph {idx+1}: Sending to ChatGPT -> {para.text}")
# Wait until the chat box is loaded
chat_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="prompt-textarea"]'))
)
# Send the paragraph to ChatGPT for editing
chat_box.click()
chat_box.send_keys(f"Please edit this text. Do not say anything before or after, only return the corrected text: {para.text}")
chat_box.send_keys(Keys.RETURN)
# Wait for the answer (10 seconds)
sleep(10)
# Get the response text from the last message
responses = driver.find_elements(By.XPATH, '//*[contains(@class, "markdown prose")]')
if responses:
last_response = responses[-1].text.strip()
if last_response:
print(f"Response text: {last_response}")
# Replace the paragraph in Word with the response
para.text = last_response
print(f"Paragraph {idx+1}: Replaced in Word file.")
# Save the Word file after processing each paragraph
save_path = r"C:\Users\Moein\Documents\new_test.docx" # New save path
doc.save(save_path)
print(f"Paragraph {idx+1}: Word file saved at {save_path}")
# Print Word file contents to confirm saving
print("Word file contents after saving:")
doc_reloaded = Document(save_path) # Reopen the file
for para in doc_reloaded.paragraphs:
print(para.text)
کسبچی | توسعه کسب و کار اینترنتی