binary-husky/gpt_academic · error · RuntimeError
请先将.doc文档转换为.docx文档。
Error message
请先将.doc文档转换为.docx文档。
What it means
For files whose extension is not docx, Word_Summary tries to open them through pywin32's Word.Application COM automation. The bare except hides the real error and raises a request to convert .doc to .docx. Therefore it fires for every non-docx path where COM is unavailable or Word cannot open the file, not only for actual .doc files.
Source
Thrown at crazy_functions/Word_Summary.py:30
for index, fp in enumerate(file_manifest):
if fp.split(".")[-1] == "docx":
from docx import Document
doc = Document(fp)
file_content = "\n".join([para.text for para in doc.paragraphs])
else:
try:
import win32com.client
word = win32com.client.Dispatch("Word.Application")
word.visible = False
# 打开文件
doc = word.Documents.Open(os.getcwd() + '/' + fp)
# file_content = doc.Content.Text
doc = word.ActiveDocument
file_content = doc.Range().Text
doc.Close()
word.Quit()
except:
raise RuntimeError('请先将.doc文档转换为.docx文档。')
# private_upload里面的文件名在解压zip后容易出现乱码(rar和7z格式正常),故可以只分析文章内容,不输入文件名
from crazy_functions.pdf_fns.breakdown_txt import breakdown_text_to_satisfy_token_limit
from request_llms.bridge_all import model_info
max_token = model_info[llm_kwargs['llm_model']]['max_token']
TOKEN_LIMIT_PER_FRAGMENT = max_token * 3 // 4
paper_fragments = breakdown_text_to_satisfy_token_limit(txt=file_content, limit=TOKEN_LIMIT_PER_FRAGMENT, llm_model=llm_kwargs['llm_model'])
this_paper_history = []
for i, paper_frag in enumerate(paper_fragments):
i_say = f'请对下面的文章片段用中文做概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{paper_frag}```'
i_say_show_user = f'请对下面的文章片段做概述: {os.path.abspath(fp)}的第{i+1}/{len(paper_fragments)}个片段。'
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
inputs=i_say,
inputs_show_user=i_say_show_user,
llm_kwargs=llm_kwargs,
chatbot=chatbot,
history=[],
sys_prompt="总结文章。"View on GitHub (pinned to d6bde0fa54)
Solutions
- Convert the file to .docx before upload, preferably on a machine with Word or with LibreOffice.
- On a server, run: soffice --headless --convert-to docx file.doc, then upload the result.
- On Windows, install Microsoft Word and pywin32 and ensure Word can open without a modal dialog.
- Use os.path.abspath(fp) instead of os.getcwd() + '/' + fp.
- Log the original COM exception so the actual cause is visible.
Example fix
# before doc = word.Documents.Open(os.getcwd() + '/' + fp) # after doc_path = os.path.abspath(fp) doc = word.Documents.Open(doc_path)
Defensive patterns
Strategy: validation
Validate before calling
import sys
ext = os.path.splitext(fp.lower())[1]
if ext != ".docx":
if sys.platform != "win32":
raise ValueError("Convert .doc to .docx before upload on non-Windows systems")
try:
import win32com.client # noqa
except ImportError as e:
raise ValueError("pywin32 is required for .doc support") from e
Type guard
def is_supported_word_input(fp: str) -> bool:
return os.path.isfile(fp) and os.path.splitext(fp.lower())[1] == ".docx"
Try / catch
try:
yield from 解析docx(...)
except RuntimeError as e:
if "转换为.docx" in str(e):
convert_with_libreoffice_or_prompt_user(fp)
else:
raise
Prevention
- Upload .docx instead of legacy .doc whenever possible.
- Do not enable COM-based .doc handling on Linux containers.
- Use absolute paths for Word Documents.Open.
- Log the original COM exception before replacing it.
When it happens
Trigger: Running on Linux/macOS or in Docker without Microsoft Word; pywin32 is not installed; Word is not activated or cannot start headless; os.getcwd()+'/'+fp is not a valid absolute/relative path; the document is protected, corrupt, or not really a .doc.
Common situations: Deploying gpt_academic in Docker; uploading legacy .doc files to a server; uppercase .DOC handling; another Word COM instance or dialog blocks Dispatch/Open; insufficient filesystem permissions.
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/617aaf11e7a61808.
Report an issue: GitHub.