Create word documents with Python | python-docx 2023

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ธ.ค. 2024

ความคิดเห็น • 37

  • @python360
    @python360  2 ปีที่แล้ว +1

    00:00 Intro
    01:37 Create Document instance
    02:19 DOCX Custom header
    04:22 Custom image
    07:03 Test Example
    09:22 Save Docx as PDF (needs Microsoft Office installed)!!

  • @philipberthiaume2314
    @philipberthiaume2314 ปีที่แล้ว +1

    Thanks for this video. I have Windows and Office installed and the pdf convert library works fine.

    • @python360
      @python360  ปีที่แล้ว

      Excellent, glad it does work. Thanks for confirming!

  • @divyarathnakar4245
    @divyarathnakar4245 10 หลายเดือนก่อน +1

    this is a v useful. Looking for the code that can set the font size and allignment in the header text. Also can we have the header set only to the first page through the script

    • @python360
      @python360  10 หลายเดือนก่อน

      This is all I found : python-docx.readthedocs.io/en/latest/user/hdrftr.html

  • @JeffGordon-x7t
    @JeffGordon-x7t ปีที่แล้ว +1

    Hello,
    I am creating my word document out of Jupyter Notebooks and it is not saving anywhere. I do not see on the documentation whether python-docx supports Jupyter but it executes without an issue/error. I have searched my files as well. Would you have any insight?
    Note: Thank you for the video! Everything was easy to follow

    • @python360
      @python360  ปีที่แล้ว +1

      Hello, pretty sure it should run fine with Jupyter, can you enter this in a cell: !pwd
      And run just that cell?
      (Assuming you are on Mac or Linux) might be cwd or something else on windows.
      Then you'll know the directory where the files are being saved to.
      Often it's best to mkdir and cd to it and start jupyter from the directory you made using terminal/powershell with "jupyter notebook"

  • @jhillyt
    @jhillyt ปีที่แล้ว +1

    Great info. Do you have anything for parsing word doc?
    Thanks for sharing

    • @python360
      @python360  ปีที่แล้ว +1

      What are you parsing it for?
      You can manipulate it as xml if you need to : superuser.com/questions/278260/how-do-i-see-the-xml-of-my-docx-document

  • @msaiff322
    @msaiff322 ปีที่แล้ว +1

    Thanks! If u don't mind, installing lxml doesn't work on macOS => error: legacy-install-failure.

    • @python360
      @python360  ปีที่แล้ว

      Ok, Thanks for the info ( question to any viewers reading this who have a mac who can solve this, drop us a message))

  • @bobvance9519
    @bobvance9519 8 หลายเดือนก่อน +1

    How can I create a hyperlink in a word document using python-docx?

    • @python360
      @python360  8 หลายเดือนก่อน +1

      I can't remember, (its v late in the UK) but the alternative would be to use 'python-docx-tpl' and do it that way.
      Use pip install and then add your own variable placeholders using jinja syntax.

  • @henryvaldivia3355
    @henryvaldivia3355 8 หลายเดือนก่อน +1

    Im trying to recreate from zero a complete template with python-docx like a resume filling its content, the body is ok but the problem is that i need to add in the header section an image like a background to cover all the header and text is front of this image. How can i achieve this?

    • @python360
      @python360  8 หลายเดือนก่อน +1

      You might be better using "python-docx-template" pip install docxtpl - so that you can start with a template file with an image already present that you put in once, manually and then reference that template and use the corresponding jinja syntax where you need to modify paragraphs etc

    • @henryvaldivia3355
      @henryvaldivia3355 8 หลายเดือนก่อน +1

      @@python360 thanks for the response. Im seeing your videos and i have this problem: as i see with python-docx-template i can replace names with curlys but in my case there are no curlies and also i have some bullet list as sample that i dont need It. My template IS a resume as you know there IS a section "skills" with these bullets sample, i need to eliminaré that list and populate with my own list of skills and also i have a work experience section list with position, dates, company and descripción (same with sample that i don't need It). Also the header section mentioned in the last comment. What can you recomend or It IS posible to work all these with python-docx-template instead DOCX?

    • @python360
      @python360  8 หลายเดือนก่อน

      @@henryvaldivia3355 Yes, so "Template" means "The word document you want to use with some variables" in this case?
      The word "template" in "docxtpl" is just a standard word/docx file, it's not a template file in the microsoft sense. This means you create your word doc as you would manually, and then add the jinja curly braces only where you need a variable?
      Forgive me if I not understood correctly, but I'd go with pip install docxtpl rather than pip install docx

  • @pippamilles2488
    @pippamilles2488 10 หลายเดือนก่อน +1

    Thank you for this video which is just what I was looking for. I am running the program in replit on an iMac. The demo.docx file won't open from within replit and when I download it, I get this error message: "Word found unreadable content in demo.docx. Do you want to recover the contents of this document? If you trust the source of this document, click Yes." When I try to recover the contents I get this error message: "Word experienced an error trying to open the file.
    Try these suggestions.
    * Check the file permissions for the document or drive.
    * Make sure there is sufficient free memory and disk space.
    * Open the file with the Text Recovery converter.
    (demo.docx)"
    Any ideas why it's not working for me? I have Microsoft Office installed.

    • @python360
      @python360  10 หลายเดือนก่อน +1

      Hi Pippa, thanks for the comment. Pretty sure we can discount insufficient memory/free disk space message. I've not used replit much, are you on a company laptop that's "locked down" ? - If you're not, then I'd try using python locally, from your laptop.
      If you have to use replit then maybe try open the docx file with vim, and at least you'll see you can read/write to the file. Once you know you can read/write then you can try establish if there is some weird UTF problem.

    • @pippamilles2488
      @pippamilles2488 9 หลายเดือนก่อน +1

      Thanks@@python360 for your advice. Your program and mine work when using Python locally so must be a limitation of replit. Very happy to have found your channel😊

  • @BoldonBigLad
    @BoldonBigLad ปีที่แล้ว +1

    I have also been using python-docx-template to create a new document based on an existing document. The document already contains content in a specific layout and I want to jump to a number of bookmarks and place content there. Is there any way to do this?

    • @python360
      @python360  ปีที่แล้ว

      Thanks! - Have you tried using Jinja curly braces as per the docxtpl video and then matching the placeholder names in the docx file with your desired paragraphs from python (created as string variables in Python)..

  • @kripapaika8374
    @kripapaika8374 ปีที่แล้ว +1

    Hi, I used jinja for creating doc template and now I can generate the doc with the required updates. But I doubt how to add configurable parameters in the template? and how can I add different headers and fonts as per user requirements at the time of doc generation by using old template?

    • @python360
      @python360  ปีที่แล้ว

      Hello, so maybe experiment with this also : github.com/elapouya/python-docx-template#introduction they both do slightly different things but between the 2 you might get what you need.

    • @kripapaika8374
      @kripapaika8374 ปีที่แล้ว +1

      @@python360 Sir, can you please do a tutorial video, please? I didn't get the idea.

    • @python360
      @python360  ปีที่แล้ว

      @@kripapaika8374 th-cam.com/video/0UvAXp9_6Jc/w-d-xo.html

  • @pribislavice
    @pribislavice 2 ปีที่แล้ว +1

    how can I change the document orientation to landscape? I've tried to use the WD_ORIENT but it doesnt work :/

    • @python360
      @python360  2 ปีที่แล้ว +1

      Ah yes : "Whilst the page is correctly tagged as landscape, its dimensions remain the same as before and must be manually changed."
      Try this : stackoverflow.com/a/37681150

    • @pribislavice
      @pribislavice 2 ปีที่แล้ว +1

      @@python360 thank you so much!

    • @python360
      @python360  2 ปีที่แล้ว

      That's ok, glad it helped you !

    • @python360
      @python360  2 ปีที่แล้ว

      ps. I made this video for you, th-cam.com/video/1blBGQwrmas/w-d-xo.html

  • @johanneschristiansilaen4215
    @johanneschristiansilaen4215 ปีที่แล้ว

    the question is, can we just create where is diffrent some title on file existing

    • @python360
      @python360  ปีที่แล้ว

      Can you elaborate? If you just want to change title and nothing else then docxtpl is a good choice as well

  • @noelle_mj3279
    @noelle_mj3279 2 ปีที่แล้ว +1

    Hi, would it be possible to get the script of the video somewhere ? Thanks. I would like to generate a word document template and change some information from an excel file : how can I do it with python ? Many thanks @Pyhon 360.

    • @python360
      @python360  2 ปีที่แล้ว +2

      Hi Noelle, yes, the code is on GitHub : github.com/RGGH/Python/blob/master/automate_stuff/doc_x.py
      (link in description also).
      Have a look at this video as well : th-cam.com/video/_BVr0h3rlGI/w-d-xo.html

  • @greenjoyce3001
    @greenjoyce3001 ปีที่แล้ว +1

    More good stuff. 👍