ruby-docx / docx

a ruby library/gem for interacting with .docx files
MIT License
439 stars 171 forks source link

undefined method `close' for nil:NilClass #100

Closed JonasHelmoe closed 3 years ago

JonasHelmoe commented 3 years ago

I wrote this simple code:

require 'docx'

# Create a Docx::Document object for our existing docx file
doc = Docx::Document.open("a.docx")

# Retrieve and display paragraphs
doc.paragraphs.each do |p|
  puts p
end

...and keep getting "undefined method `close' for nil:NilClass".

I am sure the file is there, and tried many different locations for the file, so I'm suspecting this gem is not maintained.

Can anyone guide me? Thank you

JonasHelmoe commented 3 years ago

Sorry, it worked when I used a real docx file. I just changed the file extension to "docx", but only after using a file from here, it worked: https://file-examples.com/index.php/sample-documents-download/sample-doc-download/

85636682 commented 4 months ago
file_path = File.join(Rails.public_path, '/files/file-sample_100kB.docx')
    ExceptionTrack::Log.create(title: "file_path", body: "#{file_path}")
    content = File.read(file_path)
    doc = Docx::Document.open(content)

@JonasPreisler it not worked when I used a real docx file with your ,how did you do that