【问题标题】:Grabbing the text from .doc, .docx, and rtf files and displaying them into a text field (Rails)从 .doc、.docx 和 rtf 文件中获取文本并将它们显示到文本字段 (Rails)
【发布时间】:2012-10-21 02:09:51
【问题描述】:

我有一个Post 模型:

  create_table "posts", :force => true do |t|
    t.string   "title"
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at",                            :null => false
    t.datetime "updated_at",                            :null => false
    t.integer  "comments_count",     :default => 0,     :null => false
    t.boolean  "published",          :default => false
    t.datetime "published_at"
    t.boolean  "draft",              :default => false
  end

这是它的形式:

<%= form_for(@post, :html => { :multipart => true }) do |f| %>
  <%= render 'shared/error_messages' %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content %>
  </div>

  <div class="field">
    <%= f.label :draft %>
    <%= f.check_box :draft %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我正在寻找一种从 .doc, .docx, and rtf 文件中获取文本并将它们显示到 content 文本字段中的方法(这样用户就不必打开他的文件、复制并将文本粘贴到形式)。

有什么建议吗?

(是否有任何 gem、文本编辑器或 jQuery 插件可以做到这一点?)?

编辑:

卡在这里:

post.rb:

class Post < ActiveRecord::Base
  require 'docx'
  .
  .
  .  
  def read_docx
    d = Docx::Document.open(self.document)
    d.each_paragraph do |p|
      puts d
    end
  end
end

posts_controller.rb:

class PostsController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index]
  .
  .
  .
  def create
    @user = current_user
    @post = @user.posts.new(params[:post])
    @doc_text = (no idea what to do here)

    if @post.save
      redirect_to @post, notice: 'post was successfully created.'
    else
      render action: "new"
    end
  end

  def edit
    @post = Post.find(params[:id])
  end
  .
  .
  .

posts/new.html.erb:

<%= form_for(@post, :html => { :multipart => true }) do |f| %>
  <%= render 'shared/error_messages' %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content, :value => @doc_text %>
  </div>
  .
  .
  .

我已经制作了回形针上传 docx 文件

我创建了一个名为:document的新字段

【问题讨论】:

    标签: ruby-on-rails forms doc


    【解决方案1】:

    我刚试过docx gem,效果很好。您可以在其 github 页面上获得 2 个示例。遗憾的是它不适用于 doc 文件。

    对于他们,您可以使用此 gem here。 github 页面上有一些示例,但是如果您想获取 doc 文件的全部内容,只需这样做:

    require 'msworddoc-extractor'
    
    MSWordDoc::Extractor.load('sample.doc') do |doc|
      puts doc.whole_contents
    end
    

    您还可以通过其他方法调用doc,例如documentheader。再次检查 github 页面。

    对于 rtf,您也可以使用 gem

    现在,在content 中传递它很容易。只需定义如何从文件中获取数据,例如在控制器上调用的外部库:

    @doc_text = Parser.doc("file.doc")
    @docx_text = Parser.docx("file.docx")
    @rtf_text = Parser.rtf("file.rtf")
    

    或者直接或通过您想到的任何方法获取值。要在视图中显示它,您只需添加 :value 选项,如下所示:

    <%= f.text_area :content, :value => @doc_text %> 
    #Where @doc_text is the data from file
    

    【讨论】:

    • 非常感谢!对不起,我是这方面的初学者。我必须把上面的代码放在哪里?控制器(哪个动作?)或模型或函数? (我只知道将表单的代码放在视图中的哪个位置)。我现在正在尝试 docx。
    • 取决于您想要达到的目标。您可以在任何地方实现它,但是遵循良好的模式,如果您想在用户访问表单时加载文件,我认为您能做的最好的事情是在 lib 目录中创建一个外部库,将其包含在您的控制器中并调用加载文件的方法。但是,如果您觉得很难,请在控制器模型中实现方法并像我一样在控制器中调用它。
    • 如果你想尝试这些 gem,我建议你在终端中打开一个 irb 并在那里玩,因为示例简短而简单。
    • 对不起,哈是我第一次使用这样的东西。我中途卡住了(不确定我是否做对了)。请看我的编辑
    • 如果你想在用户访问表单时显示文本,你必须在new而不是create中创建方法。你现在应该打电话给Post.read_docxinside new
    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    相关资源
    最近更新 更多